Updating workspace variables using GUIDE

I am trying to create a GUI using 'GUIDE' and have a set of workspace variables that I would like the GUI to update. I have 25 variables that I would like the user to update once they press an update button. Ideally, they would open the GUI, enter in 25 user inputs into separate 'edit text' boxes , followed by a click of the update button and then that should update the workspace variables.
I hope you can help!

12 Kommentare

You can use
doc assignin
as far as I am aware, though I have never felt the need to have my variables in the main workspace when I have a GUI as I just use the workspace of a GUI function or a function called from the GUI to work with the variables.
Stephen23
Stephen23 am 27 Jul. 2018
Better: put the 25 variables into one array. Pass that array to the GUI as an input argument, do whatever you want with it inside the GUI, then return it as output argument. Simpler, neater, more efficient, less buggy code through better code and data design!
Jan
Jan am 27 Jul. 2018
Stephen's comment is such useful, that it is worth to be an answer. Poking variables into the base workspace is a confusing indirection. It is nearly impossible to debug such constructions, while Stephen's method is compact and clear, easy to maintain and to debug.
Craig Saunders
Craig Saunders am 27 Jul. 2018
Thank you all! I really appreciate the help! I'm completely new to this so would it be possible to show an example code please?
Many thanks! Craig
Adam
Adam am 27 Jul. 2018
It does depend how connected the variables are as to whether an array or a struct is better. Since you called them all 'apples' initially it's impossible to say, but if they are things like 'velocity', 'mass', 'pressure', or whatever then I would favour a struct, whereas if they are more obviously variables that you would be tempted to name a1, a2, a3, etc then an array with indexing is definitely best.
I don't think I've ever had an algorithm with 25 distinct inputs so it's hard to say what they might be!!
Craig Saunders
Craig Saunders am 27 Jul. 2018
Hi Adam, my ultimate goal would be to allow the user to enter such things like 'mass', 'velocity' 'density' etc... click the update button and this would update the variables used within a Simulink model.
Cheers, Craig
Adam
Adam am 27 Jul. 2018
In that case I would use Stephen's technique, but using a struct with named fields rather than an array where having to decode which element of the array is mass, velocity or density etc would be a pain and very opaque for reading the code.
doc inputdlg
may be good enough for what you want. It returns a cell array, but you can just use e.g.
output = inputdlg( variableNames,... )
result = cell2struct( output, variableNames );
to get a struct out of this.
It isn't customisable in terms of positioning the elements on the UI or adding anything extra, but it is usable for just getting the user to input a number of values without too much effort on your part.
Craig Saunders
Craig Saunders am 27 Jul. 2018
Thank you Adam, Where do I put this line of code, and what areas can I change? I must reiterate I'm completely new to this. Craig
Adam
Adam am 27 Jul. 2018
This would replace your GUIDE GUI if you use inputdlg. Just put your variable names into a cell array. I'm using the terminology 'variables' here referring to variables of some algorithm - i.e. the things you are putting on your UI, rather than linking to 'variables' in the workspace as what you will end up with is a single struct, containing all these, not a boat load of individual variables.
Craig Saunders
Craig Saunders am 27 Jul. 2018
Thanks Adam! I'll give this a try - ideally I would like to use GUIDE though as I can make the user interface customisable fairly quickly.
I understand GUIDE creates an auto-generated code which then needs to be updated with how the buttons and 'edit text' fields function. It would be great to see an example code which allows a user to enter a value into the 'Edit Text' field, hit the button, which will then trigger a change in the workspace variable. At present I have been able to enter a value within GUI.m which changes the workspace variable, however I want this value entered into the 'User Input' field of the GUI instead.
Cheers.
Rik
Rik am 27 Jul. 2018
I would suggest not using GUIDE in the first place (even for GUI design), but rather read the doc for uicontrol. The values you enter are stored in the String property of each edit field. The callback for the button needs to convert them to numeric values and put them in a struct output.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Produkte

Version

R2016a

Gefragt:

am 27 Jul. 2018

Kommentiert:

Rik
am 27 Jul. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by