Get latest value from workspace variable in callback

I am coding my own gui. I could not understand how to get the control I wanted from guide. In my coding: I think I declared a global variable (such as myvar) and gave it an initial value. I started a script and declared a variable, then moved to a function file.
I changed the value via a callback from one uicontrol. The value for the variable changes in the workspace. I use a pushbutton gui to save the information, 'Callback', {@gsave, myvar} but the function gsave(hObject, eventdata, myvar) grabs the initial value of myvar and not the latest. How do I refresh the definition? (Thanks for any input.)

댓글 수: 1

Jan
Jan 2011년 3월 3일
We cannot find the mistake in your code, if you do not post it here.

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

 채택된 답변

Paulo Silva
Paulo Silva 2011년 3월 3일

0 개 추천

1- Avoid using global variables, there are several better ways to share data, use arguments to share data or in your case (the title message) you can use myvar = evalin('base', 'myvar') to get the workspace value of myvar into your gui functions.
2- When declaring global variables you must declare them global in all the functions you want to use them
fun1()
global myvar
end
fun2()
global myvar
end

댓글 수: 5

Tony
Tony 2011년 3월 3일
Thanks, I need to investigate evalin(). I also edited my question, because I am concerned that I mis-spoke when I said that I declared a global variable.
send the data to other function in other files using their arguments, it's the best way
function mygui
myvar=1;
OtherFunctionOnOtherFile(myvar)
end
Inside the GUI share the data using the handles structure.
Tony
Tony 2011년 3월 3일
The evalin() function is exactly what I need.
(If there is a similar function to write to the base, I will be able to make some of my coding work with less lines).
Thanks
assignin('base', 'myvar', myvar)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

태그

질문:

2011년 3월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by