MATLAB Answers

T Rockett
0

I am trying to make a GUI that allows a user to push a button which smooths a column in a vector and plots the result on the axes. However my GUI

Asked by T Rockett
on 15 Jan 2015
Latest activity Answered by Geoff Hayes
on 16 Jan 2015
I am trying to make a GUI that allows a user to push a button which smooths the vector N and plots the result on the axes as a function of time. The two vectors are already in my workspace, however Matlab can't see them. Here is the error message I get:
Undefined function or variable "N".
Error in gui_test2/smooth1_callback (line 12)
N(:,2) = smooth(N(:,2));
Error while evaluating UIControl Callback
And here is my code:
function gui_test2
f = figure('Visible','off','Position',[400,400,700,500]);
smooth1 = uicontrol('Style','pushbutton',...
'String','Smooth',...
'Position',[10,160,200,50],...
'Callback',{@smooth1_callback});
ha = axes('Units','pixels','Position',[250,50,400,400]);
f.Visible = 'on';
function smooth1_callback(source,eventdata)
N(:,2) = smooth(N(:,2));
plot(time,N(:,2))
end
end

  0 Comments

Sign in to comment.

1 Answer

Answer by Geoff Hayes
on 16 Jan 2015
 Accepted Answer

Captain Qwark - you indicate that the two vectors N and time are in your workspace...but you don't provide any way for your GUI to access them. Rather than relying on variables with specific names that must exist in your workspace, why not just pass them in as input parameters to your function? Just change your function signature to
function gui_test2(time,N)
and call the above from the Command Line as
>> gui_test2(time,N);
However, if you really wish to get access to these variables (perhaps you have some other set of code that is changing/updating them), then you could do the following in your callback
function smooth1_callback(source,eventdata)
if evalin('base','exist(''N'',''var'')') && ...
evalin('base','exist(''time'',''var'')')
N = evalin('base','N');
time = evalin('base','time');
N(:,2) = smooth(N(:,2));
plot(time,N(:,2))
end
end
We use the evalin function to evaluate an expression in the base workspace. We check for existence of the variables N and time using exist, and then if both do exist in the base workspace, then we assign them to our local variables which we then make use of to plot the data.
Either of the above will do what you want, but you may want to ask yourself why you must read the data from the workspace and not just provide the data to the function/GUI when you launch it.

  0 Comments

Sign in to comment.