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

1 view (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 12 Oct 2019
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!