How to: Sharing variable from one function to another function in GUI

조회 수: 2 (최근 30일)
adi kul
adi kul 2016년 6월 6일
답변: Walter Roberson 2016년 6월 6일
Hello All, I am working on a GUI where I want to take input from user and use the variable in another function which is in a separate .m file.
I tried defining the variable as global but still the file where I want to call the value returns "undefined" error.
selection = get(hObject, 'Value');
switch selection
case 1
M2.h=msgbox('Please select the value');
case 2
M2.x2=1;
case 3
M2.x2=2;
case 4
M2.x2=3;
prompt = {'Value1','Value2'};
dlg_title = 'Properties';
num_lines = 1;
defaultans = {'',''};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
M2.Value1=str2num(answer{1});
M2.Value2=str2num(answer{2});
end
set(handles.value_type, 'UserData', M2);
Now I want to call the Value1, Value2 in a separate .m file which runs in the script where above code is. Meaning the above code is a part of GUI which calls the functions from the another m file where I want the above input to be used.

답변 (2개)

Walter Roberson
Walter Roberson 2016년 6월 6일

Azzi Abdelmalek
Azzi Abdelmalek 2016년 6월 6일
You can pass them as arguments

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by