MATLAB Answers


Real time plotting - Matlab GUI

George Diamond さんによって質問されました 2019 年 2 月 20 日
最新アクティビティ George Diamond さんによって コメントされました 2019 年 3 月 4 日
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.

1 件の回答

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.

  10 件のコメント

George Diamond 2019 年 2 月 20 日
Hey Geoff,
I used datenum and it worked with a few modifications! Thanks so much for all your help. I'm gonna go ahead and accept the answer.
Geoff Hayes
2019 年 2 月 20 日
Glad it worked out, George!
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.

サインイン to comment.

Translated by