GUI Handle not always updating
2 views (last 30 days)
Show older comments
I have made a GUI where I am dynamically generating a name for saving a plot. The name is determined by inputs the user puts in to different edit boxes. However, the most frequently edited edit box where I ask the user to put in a "mode" or specific characteristic of the plot being saved, isn't always caught by the program and used for the file name.
For example, in this edit box I may put something to the affect of "ac mode_Port 1", and in the callback for the edit box I get what I input, but sometimes the previous string I input into the edit box is used when the plot saves.
I have done some debugging, and I know that my edit box callback is getting the correct string and that it is being saved to the handles.Mod variable I create. But When I call handles.Mod in another function for saving it does not always work. This error happen inconsistently, infrequently, and I am unable to reproduce it on my own accord.
Here is my code for my edit box call back...
function edit_Mod_Callback(hObject, eventdata, handles)
Mod = get(hObject,'string');
handles.Mod = Mod;
display(handles.Mod);
guidata(hObject,handles)
The display in this code always prints in the command window my expected string.
The following code is where i call "handles.Mod".
if true
function pb_SvScn_Callback(hObject, eventdata, handles)
String = StrSv(handles.BW,handles.CF,handles.Mod);
display(handles.Mod);
j=('.gif');
FileName=strcat(handles.TT,String,j);
saveName = strcat(handles.Path,'\',FileName);
h = findobj('Tag','pb_GetImg');
data = h.UserData;
imwrite(data.x,data.map,saveName);
d = datestr(now,'HH:MM AM');
str1 = ' was saved to the specified folder at ';
update = strcat(FileName, str1, d);
set(handles.text_Update, 'String', update);
end
The "display(handles.Mod)" in the function "pb_SvScn_Callback" sometimes prints the previous string I input for the last plot I saved.
I'm unsure what the issue may be. Perhaps in my call for handles.Mod I am doing something improper.
Any information or suggestions would be most appreciated.
Thanks so much.
-Jason
0 Comments
Accepted Answer
Image Analyst
on 10 Apr 2016
Do not use a callback for the edit field. It's not needed. In any other function, simply get the value from the edit field whenever you need it. Again, no code at all is needed in the callback itself for the edit text box. You don't even need handles.mod (which you want to be a copy of handles.edit_Mod.String) unless you want to pass it into a function which is not a callback but some private custom function you wrote, and you are not showing that here, so you don't need handles.mod nor a edit text box callback.
2 Comments
Image Analyst
on 10 Apr 2016
Like I said, you can use handles.edit_Mod.String.
If you have an antique version of MATLAB where that doesn't work, use get
editFieldContents = get(handles.edit_Mod, 'String'); % Works in all versions.
If you have R2014b or later, you should use handles.edit_Mod.String. If you want to save it in a differently named variable, you can do
editFieldContents = handles.edit_Mod.String; % R2014b and later.
More Answers (0)
See Also
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!