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
Adam
2018년 7월 27일
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
2018년 7월 27일
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
2018년 7월 27일
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
2018년 7월 27일
Adam
2018년 7월 27일
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
2018년 7월 27일
Adam
2018년 7월 27일
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
2018년 7월 27일
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
2018년 7월 27일
Adam
2018년 7월 27일
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
2018년 7월 27일
Rik
2018년 7월 27일
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.
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!