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!
How do I call a plot from a function in a GUI?
8 views (last 30 days)
Show older comments
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);
0 Comments
Accepted Answer
Stephanie
on 6 Dec 2012
1 Comment
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.
More Answers (1)
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!
See Also
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!