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

조회 수: 13 (최근 30일)
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.

채택된 답변

Walter Roberson
Walter Roberson 2019년 10월 12일
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
Milos Krsmanovic
Milos Krsmanovic 2019년 10월 13일
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.
Walter Roberson
Walter Roberson 2019년 10월 13일
while true
[...]
drawnow();
current_text_input = handles.fieldTag.String;
[...]
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by