MATLAB Answers

0

How to let a function know that a variable in another function has been updated?

Asked by Milos Krsmanovic on 12 Oct 2019
Latest activity Commented on by Walter Roberson
on 13 Oct 2019
Hi all, my first post here.
I'm just learning Matlab and I stumbled on a problem I'm unable to resolve on my own.
I have a GUIDE dialog box with a text input field. I am able to catch changes/user inputs in that text field via fieldTag_Callback function.
I have another function that is using that same input to perform certain calculations. The problem I have is I do not know how to let the latter function know that there has been an update in the fariable in the former function. I'm wondering which functions or methods I should use to "push" a notification to the second function so it would activate and redo whatever it needs to do, based on the new, updated value in the first function. The problem right now is, the second function will just do what it started doing the first time it was invoked, regardless if the change is initiated or not.
Not sure if I managed to explain what I'm trying to achieve properly, so please let me know.
Thanks.

  0 Comments

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 12 Oct 2019
 Accepted Answer

You do not usually need to notify a function that there has been an update.
If the function runs once and stops, then you would update the variables that the function uses to do its computation.
If the function is running in a loop, then it would need to have a pause() or drawnow() to permit itself to be interrupted by a callback . If it is using shared variables then it might need to update any deliberately remembered version of a shared variable, but more often it would just use the shared variable and immediately get the updated version. If the looping function is using values associated with a GUI, such as the choice of selection from a popup list, then after it calls pause() or drawnow() it should fetch the current value of the GUI element it is relying on.
If you have a function that looks at the current values of variables and updates something on the screen, then you can have the callback that updates the variables call the function directly.
[...]
handles.EditBox1.String = num2str(NewLatitude);
update_subway_map(hObject, [], handles) %call the update function directly

  2 Comments

Thank you for your reply.
The second function is a loop. I'm trying to make it check the text field input before starting the loop again. But the fieldTag_Callback is in another function so right now the loop just runs on the way it started once it's started.
I'm not sure I understood all you said but I will investigate shared variables now.
while true
[...]
drawnow();
current_text_input = handles.fieldTag.String;
[...]
end

Sign in to comment.