Updating workspace variables using GUIDE
Show older comments

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 Comments
Adam
on 27 Jul 2018
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
on 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
on 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
on 27 Jul 2018
Adam
on 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
on 27 Jul 2018
Adam
on 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.
Adam
on 27 Jul 2018
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
on 27 Jul 2018
Adam
on 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
on 27 Jul 2018
Rik
on 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.
Answers (0)
Categories
Find more on Structures 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!