Real time plotting - Matlab GUI
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
George Diamond
am 20 Feb. 2019
Kommentiert: Geoff Hayes
am 21 Nov. 2019
Hey y'all. I'm running into a little problem. Can y'all please help?
I am reading values from an instrument and trying to plot these values continuously when the push button (converted it to a toggle button) is pressed. I am using Guide for my GUI.
please note that the code isn't complete yet, but I am testing the real time plotting at this point. I'm not sure how to get the while (when pressed) condition for the push_button. Also, I took the section of code within the loop to a seperate file and was able to plot it. However, Matlab plots one point at a time and I was not able to get it such that it connected the points.
Thanks.
function pushbutton11_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Establish Device (Boonton 4500C RF Power Analyzer) Communication
ni=NET.addAssembly('NationalInstruments.VisaNS');
res=NationalInstruments.VisaNS.ResourceManager.GetLocalManager.FindResources('?*').cell;
dev=NationalInstruments.VisaNS.ResourceManager.GetLocalManager.Open(res{1});
% Change device units to Watts
dev.Write('CALC:UNIT W')
grab_avg_time = datetime();
grab_avg = [];
while get(pushbutton11, 'value')
grab_avg= strsplit((string(dev.Query('FETC1:ARR:CW:POW?'))),','); %Fetch data from PM and convert to string
pause(2)
grab_avg = str2num(c(2)) %Average Power - Convert String to Number and place in array
pause(0.5)
grab_avg_time = datetime('now');
pause(0.5)
plot(grab_avg_time,grab_avg, '--*')
hold on
end
% Update handles structure
guidata(hObject, handles);
0 Kommentare
Akzeptierte Antwort
Geoff Hayes
am 20 Feb. 2019
George - whenever you call plot like with
plot(grab_avg_time,grab_avg, '--*')
you are creating a new graphics object and so its data cannot be connected to data that has been plotted previously via other graphics objects. What you may want to do instead is to create one plot graphics object and update its data on each iteration of the while loop. For example, you would create the grpahics object as
hPlot = plot(NaN, NaN, '--*);
Then, in the while loop, you would update this object as
xdata = [get(hPlot,'XData') grab_avg_time]; % update the x-data
ydata = [get(hPlot,'YData') grab_avg]; % update the y-data
set(hPlot,'XData',xdata,'YData',data);
Note that instead of using a while loop, you could use a timer. See How to plot a real time signal with axes automatically updating for an example.
12 Kommentare
Emma Donnelly
am 21 Nov. 2019
Hi George,
Was wondering if you ever found a solution to plotting more than one variable on a single graph? I am working on something similar.
Thank you for sharing your work above!
Kindly,
Emma
Geoff Hayes
am 21 Nov. 2019
Hi Emma - I think that you would need two plot objects
hPlot1 = plot(NaN, NaN, '--*);
hPlot2 = plot(NaN, NaN, '--.);
and then update the first and second plots with whatever data you want to plot on each.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Graphics Performance finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
