About callback functions; If anyone can help I will appreciate

2 次查看(过去 30 天)
I have two codes, _Fig and _Fcn as attached to the question.
Please run first code Drilling_Problem_Fig and press ok buttons.
I used callback function inside _Fcn namely @clbk that I want it to reset the values of "th" and "sp" with new values for them.
The aim of resetting the figure is that if user realized that inserted a wrong numbers he/she can clean the data and insert new data again. For this reason I used callback with same Drilling_Problem_Fcn to refresh the values.
In @clbk I used clf to clear figure and again Drilling _Problem_Fcn to show the new data. Also clearvars to remove old "th" and "sp" from work space and add new of those "th" and "sp", but it doesn't work at this stage.
How could I change my callback function to add new "th" and "sp" values to my work space after reset push button.
I appreciate if anyone can help.
  3 个评论
Jan
Jan 2017-8-11
"Doesn't work" is not useful for a discussion. Prefer to post exactly, what's going on and how this differs from your expectations.
Stephen23
Stephen23 2017-8-11
"In @clbk I used clf to clear figure and again _Fcn to show the new data. Also clearvars to remove old "th" and "sp" from work space and add new of those "th" and "sp", but it doesn't work at this stage."
That sounds pointlessly complex and inefficient. Why do you need to clear variables?

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2017-8-11
'uiresume(f5)' is a bad definition of a callback. Callbacks defines as strings are supported for a backward compatibility with Matlab 5.3 from the year 1999. They are evaluated in the base workspace (the command window), such that the variable "f5" must exist there and have the correct value. Use a function handle instead:
uicontrol('style','pushbutton','string','ok', ...
'callback', @ResumeCallback);
...
function ResumeCallback(hObject, EventData)
FigH = ancestor(hObject, 'figure');
uiresume(FigH);
end
Instead of stopping code by uiwait, you could finish the code here and insert the code for the processing in the callback of the button.
clf is tricky, because it cleans the current figure. This might be evil, if you work with multiple figure and kill another GUI. Better use the figure handle to be sure to clean the wanted figure.
function [dp,th,sp]=clbk(src,evd)
clf
clearvars th sp Lz gk
clearvars is completely useless here: It clear the variables in the current workspace. Each function has its own workspace (set of local variables), an these variables do not exist in the function clbk. After clearing them, exporting them as output is double invalid: 1. callbacks do not have outputs, 2. thevariables have been removed before, so there is nothing to export.
Avoid working with scripts instead. Convert Drilling_Problem_Fig.m to a function, then its workspace is clean automatically.
Calling Drilling_Problem_Fcn() inside Drilling_Problem_Fig is not meaning full, as far as I can see. Or at least I'm confused, because both functions are calling each other mutually.
Start with converting Drilling_Problem_Fig (which looks more like a kind of "Fcn" code, in opposite to the "_Fcn" function, which looks more like a GUI code?) to a function with the inputs dp and the outputs th and sp. Then try to insert the calls into the callback of the figure. If there are still problems afterwards, come back an post the new code and ask concerning questions.
  1 个评论
omid
omid 2017-8-11
Many thanks for your perfect answer. The code is a part of a matlab based software. Please run the first code Drilling_Problem_Fig and press the ok buttons to see. The aim of resetting the figure is that if user realized that inserted a wrong numbers he/she can clean the data and insert new data again. For this reason I used callback with same Drilling_Problem_Fcn to refresh the values. The problem as previously said is that new values for "th" and "sp" couldn't insert to work space and old values are still remain. Thanks

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by