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 commentaires

Rik
Rik le 27 Jan 2020
Also, I would suggest not using GUIDE, see this thread.
Stephen23
Stephen23 le 27 Jan 2020
As Rik wrote, it is best to avoid GUIDE and write your own GUI code.
If you must use GUIDE, then follow these instructions:
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 le 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.

Connectez-vous pour commenter.

 Réponse acceptée

Rik
Rik le 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 commentaires

Rik
Rik le 30 Jan 2020
Note: I edited this and put it here as an example.
Thank you for your reply and help, Rik. I have tried the code you wrote in the comment and run it. Now, I obtain a is the class object contains the variable I need for the next calculation. So, I need to store all variables inside class as shown below. I use parameter B to store the input variable named "Running_Inflow" from GUI. However, B seems to store the default value instead of the value I selected in GUI.
Ps, I have checked the link you post. I saw Adam's comment. I have attempted to modify based on what you suggest, but still not succeeded yet. Could you please give me some advices to fix this problem? I am so grateful for your kind help.
clc;
clear;
close all;
a=MyClass;%open the GUI
%now you can modify the GUI
%%
A=a.Running_Inflow %retrieves the property from the GUI
%%
%now close the GUI
B=a.Running_Inflow %see how this still works? Now, I use B to store variable inside class
Rik
Rik le 31 Jan 2020
You should only run the code that creates B after you have changed the GUI elements to the required values. Can you confirm you are doing that?
Your initial code was missing this aspect, it wasn't waiting for user input before retrieving the settings.
Danupon Subanapong
Danupon Subanapong le 31 Jan 2020
Thanks for your quick reply. I think it was the reason. I think that B was created before GUI elements were changed. But how to create B after I have changed the GUI elements? I am so sorry for many questions.
Rik
Rik le 31 Jan 2020
If you insist on linear execution you will need a call to uiwait. The easiest way is probably to make the fig handle a public property so you can use it.
That is the reason my code snippet was created with sections. Or are you using such an old release that it doesn't support sections?
Danupon Subanapong
Danupon Subanapong le 31 Jan 2020
Thank you so much, Rik. I use 2018b version. I think it is not that old. I will try to use uiwait and inform you if I succeed or not.
Danupon Subanapong
Danupon Subanapong le 31 Jan 2020
It really works. Thank you so much, Rik.
Rik
Rik le 31 Jan 2020
You're welcome.
And indeed R2018b is not that old. Currently I would consider anything before R2016b as old and anything before R2014b as very old. This is mostly based on the features mentioned here.
Danupon Subanapong
Danupon Subanapong le 8 Fév 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 le 8 Fév 2020
You should be able to pass values to the constructor function. Have you tried doing that?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Produits

Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by