MATLAB: GUI Callback

edit textguihandlepushbuttonsave

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

Best Answer

When you use a string as a Callback, the string is executed in the base workspace, not in the workspace of the function that set the callback.
Also, you are trying to mix command line syntax and function syntax in your save command.
set(pbh,'Callback',@(src,evt) save('data'.txt', get(Bz_eth, 'String'), '-ascii') );