Info
この質問は閉じられています。 編集または回答するには再度開いてください。
GUI and dealing with functions
2 ビュー (過去 30 日間)
古いコメントを表示
Hello, I am new to the GUI and I have a problem is that very basic. The question is regarding dropdown menu. When I select a value in dropdown menu lets say 3 and assign it to some variable lets say K. I want it to be used in other functions but other functions don't see it. I tried global variable but that doesn't work.
Can you help me. Thank you
0 件のコメント
回答 (2 件)
Image Analyst
2012 年 6 月 25 日
If you're using GUIDE, you'd do
global K;
K = get(handles.popmenu1, 'Value');
and then put global K; into every other function that needs to see K. Did you do that?
Or else you can just use that "get" line in every other function that needs K and forget about setting it to a global variable, as long as that function can see the handles structure, which all call backs can. If you need it in a function you wrote, a non-callback function, then you'd have to pass handles in via the argument list.
Alternatively, did any of the ideas in the FAQ work for you? http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
2 件のコメント
Image Analyst
2012 年 6 月 25 日
No. If it's global, you don't need to pass it via arg lists anymore. Just declare it global again inside any function that needs to "see" it.
function myfunction()
global K;
try
% Your code goes here
catch ME
errorMessage = sprintf('Error in myfunction().\nThe error reported by MATLAB is:\n\n%s', ME.message);
uiwait(msgbox(errorMessage));
end
John
2012 年 6 月 25 日
you can either, as Image Analyst states, get(handles.dropdownmenu1,'Value') during each function or you can simply store it in the structure handles.
For example if the name of the dropdown menu is dropdownmenu1. then:
handles.value = get(handles.dropdownmenu1,'Value');
%Also add the following line at the end of the function
guidata(hObject,handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Now 'handles.value' should be accessible to every other function.
0 件のコメント
この質問は閉じられています。
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!