Pass input data obtained from GUI to .m
32 views (last 30 days)
Show older comments
Hello!!!
I am very new to GUIDE. I built GUI and intend to pass variable from GUI to .m for calculation. I can get the parameter from GUI as global variable already but those parameters cannot be used after that. Can anyone help me? I attached two files include .m and GUI.
5 Comments
Stephen23
on 31 Jan 2020
Rik's answer below is a neat and effective approach by defining a simple class.
Another simple alternative uses nested functions, for example my FEX submssion iregexp:
Note the waitfor is commented out in the file, uncomment it to return control when the GUI is closed.
Accepted Answer
Rik
on 30 Jan 2020
I think it makes more sense to wrap this GUI in a class, so the class object remains after closing the GUI, which would allow you to extract the values, even after the figure is closed.
I slightly modified the code you posted and made it the class constructor. The code snippet below is what you could run.
a=MyClass;%open the GUI
%now you can modify the GUI
%%
a.Running_Inflow %retrieves the property from the GUI
%%
%now close the GUI
a.Running_Inflow %see how this still works?
The class definition:
classdef MyClass < handle
properties
%set default values (note: these are not used in the constructor)
Running_Inflow=false;
Running_Servo=false;
Running_Hydro=false;
text_file='Send';
end
methods
function obj=MyClass
%contructor
ss=get(0,'screensize');
he=600;
we=600;
figure('position',[(ss(3)-we)/2, (ss(4)-he)/2, we, he],...
'menubar', 'none');
uicontrol('tag', 'button2', 'string', 'CANCEL', ...
'position', [520,20,60,20],...
'callback', @(h,e) Callback_CANCEL(h));
uicontrol('tag', 'Inflowswitch', 'style', 'radiobutton', ...
'position', [20,500,600,20], 'String','Inflow switch',...
'Callback',@(h,e) Callback_radio1(h,e,obj));
uicontrol('tag', 'Servoswitch', 'style', 'radiobutton', ...
'position', [20,480,600,20], 'String','Servo switch',...
'Callback',@(h,e) Callback_radio2(h,e,obj));
uicontrol('tag', 'Hydroswitch', 'style', 'radiobutton', ...
'position', [20,460,600,20], 'String','Hydro switch',...
'Callback',@(h,e) Callback_radio3(h,e,obj));
uicontrol('tag','text1','style','text',...
'position', [20, 50, 560, 20], ...
'BackgroundColor', [1,0,0], ...
'ForegroundColor', 'g', ...
'String', 'Send',...
'Callback',@(h,e) Callback_text1(h,e,obj));
uicontrol('tag', 'button1', 'string', 'OK', ...
'callback',@(h,e) Callback_OK(h));
end
end
end
function Callback_radio1(h,~,class_obj)
%set the class property so it can be retrieved after the GUI has closed
class_obj.Running_Inflow=h.Value;
end
function Callback_radio2(h,~,class_obj)
%set the class property so it can be retrieved after the GUI has closed
class_obj.Running_Servo=h.Value;
end
function Callback_radio3(h,~,class_obj)
%set the class property so it can be retrieved after the GUI has closed
class_obj.Running_Hydro=h.Value;
end
function Callback_text1(h,~,class_obj)
class_obj.text_file=h.String;
end
function Callback_OK(h,~)
close(h.Parent)
end
function Callback_CANCEL(h,~)
%should the properties be reset to their default values?
close(h.Parent)
end
10 Comments
Rik
on 8 Feb 2020
You should be able to pass values to the constructor function. Have you tried doing that?
More Answers (0)
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!