GUI Callback
Show older comments
I have a question about using editable text fields and push buttons.
I am new to coding GUIs and am having trouble creating one for a Fortran program I wrote. I would like to make a GUI that takes user inputs through editable text fields and saves these values to a file (.txt., .dat. or .mat, whichever is best for Fortran). I am having trouble saving my data, as it seems that the strings from the text fields aren't saved to the workspace, so when I set the pushbutton callback, it does not find the string values from the text fields For example,
Bz_eth = uicontrol(fh, 'Style', 'edit', 'Position', [40 130 130 20]);
pbh = uicontrol(fh, 'Style', 'pushbutton', 'String', 'Run', 'Position', [525 20 50 25]);
set(pbh,'Callback','save data.txt get(Bz_eth,''String'') -ascii')
I have tried several other ways (unsuccessfully), such as writing a subfunction to save the data to the workspace and then using it in the callback, but the data isn't saved and the subfunction instead saves empty arrays (as the text fields are empty when the GUI starts). Any suggestions on how to fix my code or how to implement it better? I truly appreciate your time. Thank you
Accepted Answer
More Answers (1)
Image Analyst
on 23 Jun 2012
1 vote
Any reason you're not using GUIDE, where you wouldn't have that problem? With GUIDE, each callback function has access to the handles structure, from which you can get the value, contents, or selected item of any/all of the controls on your GUI. No need to worry that the callback won't have access to one of your handles, and so can't get or set its properties, like you're experiencing now.
5 Comments
Walter Roberson
on 23 Jun 2012
No need to worry? Hmmm. http://www.mathworks.com/matlabcentral/answers/41858-variables-in-guide-program-mysteriously-being-reset
Cesar Huerta
on 23 Jun 2012
Image Analyst
on 23 Jun 2012
If you feel like it's cheating, then go ahead and do it the hard way. But virtually no professional programmers start from the lowest levels. Do you know anyone who uses Visual Studio who does that? No - they all use the dialog box editors (though it is far far superior to GUIDE). And you'll just be wasting time (which means money) reinventing the wheel when it comes to coding up all the functions in some toolbox because using them would be cheating. And God help you if you ever need to write your own compiler because it would be cheating to use the built in compiler. In fact, maybe you should consider using MATLAB at all "cheating" because it does a lot of stuff for you. Why don't you just write everything in C or Java? I most likely would not hire a person if I found out that they wanted to build everything themselves and not take advantage of tools that can make their life simpler, get projects done faster, and make them a more productive employee. Sorry if this sounds harsh, but for your own good I really really suggest you get over that feeling so you can use built in capabilities and become more productive.
Cesar Huerta
on 24 Jun 2012
Walter Roberson
on 24 Jun 2012
"The perfect is the enemy of the good" -- Voltaire.
"Good is the enemy of great" -- Jim Collins
"good and bad coin cannot circulate together" -- Sir Thomas Gresham
"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
GUIDE is "good enough" for a number of things, but has many limitations, and it promotes use models of MATLAB GUI frozen in time; and it does not not encourage people to learn the more advanced capabilities (indeed, it tends to lull people into thinking that what GUIDE provides is all that is possible.)
GUIDE might be good enough for cases where the user experience is not a serious consideration, for research cases where the research algorithm is the only really important thing, and the user interfaces are just useful ways to trigger the research algorithm. But in a competitive environment, when the technological capabilities and price are roughly comparable, people select the choice with the better user interface.
Image Analyst works in a fairly specific field, image processing research, and pretty much only in that field. Adopting GUIDE is perhaps appropriate for his circumstances.
I work in a wide variety of fields, not a niche. I use the tools appropriate for the circumstances I am working in. GUIDE is often inappropriate for what I do. Knowing what is actually possible and the tradeoffs involved is what is appropriate for what I do.
Every sufficiently large organization needs someone who can look at how the program runs, see a flaw, and trace it back through the code, trace back through the tool that produced the code, and trace right back through the compiler that produced the tool, and say "This is wrong, and here's how to fix it." Those people get to where they do by studying how things *really* work.
Categories
Find more on Interactive Control and Callbacks 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!