Problem while creating a gui and to share variables via assignin

조회 수: 1 (최근 30일)
Hello,
I just created a gui where I'm facing a problem. Let me explain it briefly.
First, the Import button is pressed. Then a csv is read in. So that I have the imported data available in the workspace, I transfer it in the callback function with assignin to the workspace.
The variables can also be found in the workspace. Unfortunately I cannot pass them to other functions. In the appendix I have added a short minimal example in which the problem can be reproduced.
Thanks a lot for your help
clear all
fig = uifigure;
fig.Position(3:4) = [600 320];
ax = uiaxes('Parent',fig,...
'Units','pixels',...
'Position',[10 10 300 280]);
btn = uibutton(fig,...
'push',...
'Position',[400,215, 100, 22],...
'Text','Plot',...
'BackgroundColor',[0.3010 0.7450 0.20],...
'ButtonPushedFcn', @(btn,event)...
plotButtonPushed(btn,ax,...
variable_1));
% when this button is pressed, this function should get the created
% variable from the callback function
import = uibutton(fig,...
'push',...
'Position',[100,290, 100, 22],...
'Text','Import',...
'BackgroundColor',[0 0.8 0.6],...
'ButtonPushedFcn', @(btn,event) plotButtonImport(fig));
function plotButtonImport(fig)
assignin('base','variable_1',10)
% Here I create a variable and give it to the base workspace
end
function plotButtonPushed(btn,ax,value)
k=k+1;
end

채택된 답변

Spencer Chen
Spencer Chen 2020년 2월 21일
What do you mean by "pass them to other functions"? Do you mean access of variable_1 by plotButtonPushed()?
When you set the callback for plotButtonPushed(), you specified that it takes the input argument variable_1. This means that it will execute the callback with the value of variable_1 at the time that you called uibutton, not the latest value in variable_1.
To solve this issue, you can use evalin() in plotButtonPushed() to get the latest value from variable_1 from your base workspace.
But personally, it's cleaner if you can avoid putting stuff in the base workspace unless you really have to; i.e. at some stage, the base workspace needs to use that variable.
For a better programming practice, I would suggest storing variable_1 is one of the UserData fields of your GUI components, to be shared by other component. For example:
function plotButtonImport(fig)
fig.UserData = 1;
end
function plotButtonPushed(btn,ax,value)
btn.Parent.UserData = btn.Parent.UserData + 1;
end
Blessings,
Spencer
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 2월 21일
With the variable possibly not existing at the time that the anonymous function is created, you get into one of the corner cases involving behavior that recently changed in MATLAB. In some older versions, it should in such cases get the variable from the base workspace, but in newer versions, it should complain about the variable not being defined (in both cases for indirect reasons, not by direct definition!). So the release is important to know for determining exact behaviour.
... But better to Don't Do That!
Johannes Liebrich
Johannes Liebrich 2020년 2월 22일
Thank you Spencer Chen!
I just updated the values in the plotButtonPushed by evalin in my function and I can use the values which i created in the function plotButtonImport()..
Thanks a lot!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by