MATLAB Answers

Real time plotting - Matlab GUI

153 ビュー (過去 30 日間)
George Diamond
George Diamond 2019 年 2 月 20 日
コメント済み: Geoff Hayes 2019 年 11 月 21 日
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.
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
% 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
grab_avg = str2num(c(2)) %Average Power - Convert String to Number and place in array
grab_avg_time = datetime('now');
plot(grab_avg_time,grab_avg, '--*')
hold on
% Update handles structure
guidata(hObject, handles);

  0 件のコメント

サインイン to comment.


Geoff Hayes
Geoff Hayes 2019 年 2 月 20 日
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
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 件のコメント

表示 9 件の古いコメント
George Diamond
George Diamond 2019 年 3 月 4 日
Hi Geoff,
Hope all is well.
Is there a way to plot two different variables on the same axes using the method you recommended. Every time I try, it combines the results into a single plot.
Please let me know.
Emma Donnelly
Emma Donnelly 2019 年 11 月 21 日
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!
Geoff Hayes
Geoff Hayes 2019 年 11 月 21 日
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.

サインイン to comment.

その他の回答 (0 件)

サインイン してこの質問に回答します。

Translated by