Pass input data obtained from GUI to .m

32 views (last 30 days)
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
Danupon Subanapong
Danupon Subanapong on 30 Jan 2020
Thanks all for your suggestions.
As Stephen's and Rik's suggestion, I created gui without GUIDE. The code is as below. I used handles and guidata to store variables. I also created push button to close the GUI. However, after closing GUI parameters are gone as well. Is there any suggestion on how to make GUI passing data to *.mat for the next calculation in *.mat script?
ss=get(0,'screensize');
he=600;
we=600;
hfig=figure('position',[(ss(3)-we)/2, (ss(4)-he)/2, we, he],...
'menubar', 'none');
hbutton2=uicontrol('tag', 'button2', 'string', 'CANCEL', ...
'position', [520,20,60,20]);
hradio1=uicontrol('tag', 'Inflowswitch', 'style', 'radiobutton', ...
'position', [20,500,600,20], 'String','Inflow switch');
hradio2=uicontrol('tag', 'Servoswitch', 'style', 'radiobutton', ...
'position', [20,480,600,20], 'String','Servo switch');
hradio3=uicontrol('tag', 'Hydroswitch', 'style', 'radiobutton', ...
'position', [20,460,600,20], 'String','Hydro switch');
htext=uicontrol('tag','text1','style','text',...
'position', [20, 50, 560, 20], ...
'BackgroundColor', [1,0,0], ...
'ForegroundColor', 'g', ...
'String', 'Send');
Running_Inflow=get(hradio1,'Value');
Running_Servo=get(hradio2,'Value');
Running_Hydro=get(hradio3,'Value');
text_file=get(htext,'String');
handles=guihandles(hfig);
guidata(hfig, handles);
hbutton1=uicontrol('tag', 'button1', 'string', 'OK', ...
'callback', 'close(hfig)') ;
Stephen23
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.

Sign in to comment.

Accepted Answer

Rik
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
Danupon Subanapong
Danupon Subanapong on 8 Feb 2020
Hello!!!
I have got another question. I would like to pass variables from a script to gui. For example, I will have the above gui dialougue box inside the for loop to store input values for each simulation cases. In order to know what number of simulation case I am inputing in gui dialogue box, I would like to pass the current index and total simulation case to gui dialogue box. Can you give me some suggestion?
Rik
Rik on 8 Feb 2020
You should be able to pass values to the constructor function. Have you tried doing that?

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!