How do I call a plot from a function in a GUI?

8 views (last 30 days)
Hi! I created a function that plots 3 different signals and an average of all the signals onto the same plot. This function works fine when I call it with an m file, but if I call it using my GUI I cannot get the plot to show up on my axes in the GUI. I must use the same function I created to run with my m file to be called with my GUI. Below is my function (noisysignals) and my GUI code.
Thanks!
MY FUNCTION
function noisysignals(a,b,c,d,e,g)
SR=500;
time=400./1000;
G=g/1000;
NumberofPoints=time.*SR;
increment=time./NumberofPoints;
t=0:increment:time;
x=b.*(t.^e).*exp(-t./G).*cos(2.*pi.*c.*t);
noise=rand(a,length(x)).*d;
copies=repmat(x,[a 1]);
copieswithnoise = copies+noise;
Average= mean(copieswithnoise);
hold on
line1 =plot (t,copieswithnoise (1,:), 'b', 'Linewidth', 1));
line2=plot (t,copieswithnoise (2,:), 'b', 'Linewidth', 1);
line3=plot (t,copieswithnoise (3,:), 'b', 'Linewidth', 1);
line4=plot ( t, Average, 'r', 'LineWidth', 3 );
hold off
title ('Signals With Noise','FontSize', 17);
xlabel ('Time (seconds)','FontSize', 12);
ylabel ('Voltage (Volts)','FontSize', 12);
legendHandle = [line1 line2 line3 line4];
legend(legendHandle([1 4]),'Individual Signals','Average of all Signals');
end
MY GUI (The important part- I skipped over defining each of my edit boxes)
--- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
hObject handle to pushbutton1 (see GCBO)
eventdata reserved - to be defined in a future version of MATLAB
handles structure with handles and user data (see GUIDATA)
N= str2double(get(handles.copies, 'string'));
amplitude= str2double( get(handles.Amplitude, 'string'));
frequency= str2double(get(handles.Frequency, 'string'));
gamma= str2double(get(handles.Gamma, 'string'));
tau= str2double(get(handles.Tau, 'string'));
standardD= str2double(get(handles.SD, 'string'));
noisysignals (N, amplitude, frequency, standardD, gamma, tau);

Accepted Answer

Stephanie
Stephanie on 6 Dec 2012
I figured it out. It actually had absolutely nothing to do with my code. I had been entering my input A as 8.*10.^6 which worked fine in my function, but as soon as I entered it into my GUI it wouldn't work. Once I changed my A value to 8E6 it worked perfectly. Thanks for the help though!
  1 Comment
Muthu Annamalai
Muthu Annamalai on 6 Dec 2012
You can use the function, 'eval' to check the input. But I don't recommend it. You should take user input as numbers instead.

Sign in to comment.

More Answers (1)

Muthu Annamalai
Muthu Annamalai on 6 Dec 2012
Edited: Muthu Annamalai on 6 Dec 2012
When using the GUI, you should create a new figure() and save the handle to it.
h = figure();
plot(h, x_axis, y_axis, label, etc.. )
So I'd keep all the code same but pass in the handle information as first argument, and you're all set!
  1 Comment
Stephanie
Stephanie on 6 Dec 2012
Is this supposed to create an additional figure? When I tried this it created an additional figure, but I kept getting errors so the plot was blank. I need the plot to go in the axes in my GUI, not create an additional figure. Am I supposed to put that bit of code in my GUI or function?
Thanks
Here is what I have tried in my function:
h= figure (1);
hold on
plot (h,t,copieswithnoise (1,:), 'b', 'Linewidth', 1);
plot (h,t,copieswithnoise (2,:), 'b', 'Linewidth', 1);
plot (h,t,copieswithnoise (3,:), 'b', 'Linewidth', 1);
plot (h, t, Average, 'r', 'LineWidth', 3 );
hold off
And I got this error:
Error using plot
String argument is an unknown option.
Error in noisysignals (line 28)
plot (h,t,copieswithnoise (1,:), 'b', 'Linewidth', 1);
Error in RedoProblem2GUI>pushbutton1_Callback (line
230)
noisysignals (N, amplitude, frequency, gamma, tau,
standardD);
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in RedoProblem2GUI (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)RedoProblem2GUI('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
I have tried a few other variations and keep getting similar results

Sign in to comment.

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!