How to let a function know that a variable in another function has been updated?
12 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Milos Krsmanovic
il 12 Ott 2019
Commentato: Walter Roberson
il 13 Ott 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 Commenti
Risposta accettata
Walter Roberson
il 12 Ott 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.
http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
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 Commenti
Walter Roberson
il 13 Ott 2019
while true
[...]
drawnow();
current_text_input = handles.fieldTag.String;
[...]
end
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Graphics Objects in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!