Gui plot using Guide

Hi all,
I got some help with a code that lets me plot a variable in real time. However, I am having problem plotting 2 variables or inputs on th same plot. When I try to plot both inputs, it somehow combines the value and plots a single point for both. Not as famaliar with this plot method and not a 100% sure if it's even possible to do what I'm trying to.
Does you all have suggestions or other things to try?
Please let me know.
Thanks.
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.edit1.String = str2double(string(handles.dev.Query('SENS1:CORR:OFFS?'))); %Grab channel offset
handles.edit2.String = str2double(string(handles.dev.Query('SENS2:CORR:OFFS?'))); %Grab channel offset
grab_max = [];
grab_max_time = datetime();
hPlot = plot(NaN, NaN, '--*');
hasPlotBeenUpdated = false;
while handles.pushbutton2.Value == 1
grab_max= strsplit((string(handles.dev.Query('FETC1:ARR:CW:POW?'))),','); %Fetch data from PM and convert to string
grab_max = str2double(grab_max(4)); %Average Power - Convert String to Number and place in array
grab_max_time = datetime('now');
grab_max_t = datenum(grab_max_time);
pause(str2double(handles.edit3.String))
hold on
if ~hasPlotBeenUpdated
hasPlotBeenUpdated = true;
set(hPlot,'XData',grab_max_t,'YData',grab_max);
handles.grab_max = grab_max;
else
xdata = [get(hPlot,'XData'), grab_max_t]; % update the x-data
ydata = [get(hPlot,'YData'), grab_max]; % update the y-data
set(hPlot, 'XData', xdata, 'YData', ydata);
end
end
hold off

13 comentarios

Adam
Adam el 6 de Mzo. de 2019
"When I try to plot both inputs, it somehow combines the value and plots a single point for both"
I don't really understand what you mean by this. do you have screenshots, or some other way of explaining it? The general technique seems fine, glancing over the code, although you need a
guidata( hObject, handles )
at the bottom of your function to ensure handles.grab_max is updated properly, but that is not relevant to the particular problem you are describing as far as I can see.
George Diamond
George Diamond el 6 de Mzo. de 2019
Hey Adam, thanks for responding. I added the update line.
I tried forcing both input to the same plot. Please see below:
It plots another point (different value - 0 or close to 0) on the same line instead of making a second line on the same graph.
if ~hasPlotBeenUpdated
hasPlotBeenUpdated = true;
set(hPlot,'XData',grab_avg_t,'YData',grab_avg);
set(hPlot,'XData',grab_avg_t,'YData',grab2_avg); % For second input
handles.grab_avg = grab_avg;
else
xdata = [get(hPlot,'XData'), grab_avg_t]; % update the x-data
ydata = [get(hPlot,'YData'), grab_avg]; % update the y-data
set(hPlot, 'XData', xdata, 'YData', ydata);
end
Adam
Adam el 6 de Mzo. de 2019
Updating plots using the method above will add to the same line, that is its purpose as it is far more efficient to do this than to keep replotting the same data just with an extra point added.
If what you want is a seperate plot though then you will need another plot instruction instead.
George Diamond
George Diamond el 6 de Mzo. de 2019
Thanks Adam. Is that as in I need to make a second set of axes to plot to? Do you have other suggestions for what I'm trying to do, please?
Adam
Adam el 6 de Mzo. de 2019
It's up to you if you want another axes, but if you want to visualise them in the same place then you can use the same axes, just with an appropriately positioned
hold( hAxes, 'on' )
before you do a new
plot( hAxes, ... )
instruction, where hAxes is the handle of your axes.
George Diamond
George Diamond el 6 de Mzo. de 2019
Hey Adam, sorry to keep bugging you. I added a hold and tried to plot in the same space. I got the following. Any ideas how I can decouple them?
gui_plot.JPG
Adam
Adam el 6 de Mzo. de 2019
Are there supposed to be 2 plots there? I can only see one.
George Diamond
George Diamond el 6 de Mzo. de 2019
Well, that's the problem. They're both plotted on the same line. I tried to plot both lines on the same plot, but it combined the results. I believe the higher points are grab_max points and the lower point from grab2_max points.
Adam
Adam el 6 de Mzo. de 2019
If they are being plotted as two explicit plot instructions this should not happen, even if you are updating each one as you go using the above syntax, you should be updating 2 distinct line objects with the points for each.
George Diamond
George Diamond el 6 de Mzo. de 2019
Hey Adam, please see the code for the above plot below:
% --- 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)
handles.edit1.String = str2double(string(handles.dev.Query('SENS1:CORR:OFFS?'))); %Grab channel offset
handles.edit2.String = str2double(string(handles.dev.Query('SENS2:CORR:OFFS?'))); %Grab channel offset
grab_avg = [];
grab_avg_time = datetime();
grab2_avg = [];
grab2_avg_time = datetime();
hPlot = plot(NaN, NaN, '--*');
% hPlot2 = plot(NaN, NaN, '--o');
hasPlotBeenUpdated = false;
while handles.pushbutton1.Value == 1
handles.pushbutton1.BackgroundColor = '1 1 0';
grab_avg= strsplit((string(handles.dev.Query('FETC1:ARR:CW:POW?'))),','); %Fetch data from PM and convert to string
grab_avg = str2double(grab_avg(2)); %Average Power - Convert String to Number and place in array
grab_avg_time = datetime('now');
grab_avg_t = datenum(grab_avg_time);
grab2_avg= strsplit((string(handles.dev.Query('FETC2:ARR:CW:POW?'))),','); %Fetch data from PM and convert to string
grab2_avg = str2double(grab2_avg(2)); %Average Power - Convert String to Number and place in array
pause(str2double(handles.edit3.String))
hold on
if ~hasPlotBeenUpdated
hasPlotBeenUpdated = true;
set(hPlot,'XData',grab_avg_t,'YData',grab_avg);
hold(handles.axes2, 'on')
set(hPlot,'XData',grab_avg_t,'YData',grab2_avg);
handles.grab_avg = grab_avg;
else
xdata = [get(hPlot,'XData'), grab_avg_t]; % update the x-data
ydata = [get(hPlot,'YData'), grab_avg]; % update the y-data
% x2data = [get(hPlot2,'XData'), grab_avg_t]; % update the x-data
% y2data = [get(hPlot2,'YData'), grab_avg]; % update the y-data
set(hPlot, 'XData', xdata, 'YData', ydata);
% set(hPlot2, 'XData', x2data, 'YData', y2data);
end
grab2_avg= strsplit((string(handles.dev.Query('FETC2:ARR:CW:POW?'))),','); %Fetch data from PM and convert to string
grab2_avg = str2double(grab2_avg(2)); %Average Power - Convert String to Number and place in array
grab2_avg_time = datetime('now');
grab2_avg_t = datenum(grab2_avg_time);
pause(str2double(handles.edit3.String))
hold on
if ~hasPlotBeenUpdated
hasPlotBeenUpdated = true;
set(hPlot,'XData',grab2_avg_t,'YData',grab2_avg);
handles.grab2_avg = grab2_avg;
else
xdata = [get(hPlot,'XData'), grab2_avg_t]; % update the x-data
ydata = [get(hPlot,'YData'), grab2_avg]; % update the y-data
set(hPlot, 'XData', xdata, 'YData', ydata);
end
end
handles.pushbutton1.BackgroundColor = '0.3 0.75 0.93';
hold off
guidata(hObject,handles)
Adam
Adam el 6 de Mzo. de 2019
You still only seem to have
hPlot
there and you are giving it both your grab_avg and grab_avg_t data. You need to distinct plot handles to update, one for each, if I understand correctly the result you want.
George Diamond
George Diamond el 6 de Mzo. de 2019
Hey Adam. I had shown the code above to show what produced the plot I showed earlier.
I updated the code as shown below and got some errors. please see below:
% --- 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)
handles.edit1.String = str2double(string(handles.dev.Query('SENS1:CORR:OFFS?'))); %Grab channel offset
handles.edit2.String = str2double(string(handles.dev.Query('SENS2:CORR:OFFS?'))); %Grab channel offset
grab_avg = [];
grab_avg_time = datetime();
grab2_avg = [];
grab2_avg_time = datetime();
hPlot = plot(NaN, NaN, '--*');
hPlot2 = plot(NaN, NaN, '--o');
hasPlotBeenUpdated = false;
while handles.pushbutton1.Value == 1
handles.pushbutton1.BackgroundColor = '1 1 0';
grab_avg= strsplit((string(handles.dev.Query('FETC1:ARR:CW:POW?'))),','); %Fetch data from PM and convert to string
grab_avg = str2double(grab_avg(2)); %Average Power - Convert String to Number and place in array
grab_avg_time = datetime('now');
grab_avg_t = datenum(grab_avg_time);
grab2_avg= strsplit((string(handles.dev.Query('FETC2:ARR:CW:POW?'))),','); %Fetch data from PM and convert to string
grab2_avg = str2double(grab2_avg(2)); %Average Power - Convert String to Number and place in array
pause(str2double(handles.edit3.String))
hold on
if ~hasPlotBeenUpdated
hasPlotBeenUpdated = true;
set(hPlot,'XData',grab_avg_t,'YData',grab_avg);
hold(handles.axes2, 'on')
set(hPlot2,'XData',grab_avg_t,'YData',grab2_avg);
handles.grab_avg = grab_avg;
else
xdata = [get(hPlot,'XData'), grab_avg_t]; % update the x-data
ydata = [get(hPlot,'YData'), grab_avg]; % update the y-data
x2data = [get(hPlot2,'XData'), grab_avg_t]; % update the x2-data
y2data = [get(hPlot2,'YData'), grab_avg]; % update the y2-data
set(hPlot, 'XData', xdata, 'YData', ydata);
set(hPlot2, 'XData', x2data, 'YData', y2data);
end
grab2_avg= strsplit((string(handles.dev.Query('FETC2:ARR:CW:POW?'))),','); %Fetch data from PM and convert to string
grab2_avg = str2double(grab2_avg(2)); %Average Power - Convert String to Number and place in array
grab2_avg_time = datetime('now');
grab2_avg_t = datenum(grab2_avg_time);
pause(str2double(handles.edit3.String))
hold on
if ~hasPlotBeenUpdated
hasPlotBeenUpdated = true;
set(hPlot,'XData',grab2_avg_t,'YData',grab2_avg);
handles.grab2_avg = grab2_avg;
else
xdata = [get(hPlot,'XData'), grab2_avg_t]; % update the x-data
ydata = [get(hPlot,'YData'), grab2_avg]; % update the y-data
set(hPlot, 'XData', xdata, 'YData', ydata);
end
end
handles.pushbutton1.BackgroundColor = '0.3 0.75 0.93';
hold off
guidata(hObject,handles)
Error message shown below:
Error using matlab.graphics.chart.primitive.Line/set
Invalid or deleted object.
Error in Boonton_4500C_GUI_2_Continuous>pushbutton1_Callback (line 211)
set(hPlot,'XData',grab_avg_t,'YData',grab_avg);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Boonton_4500C_GUI_2_Continuous (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Boonton_4500C_GUI_2_Continuous('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
George Diamond
George Diamond el 6 de Mzo. de 2019
Hey Adam, thanks so much for all your help. I've gotten it to work. I tweaked the code a little with both plot objects. I was not handles the individual objects properly at first. Thanks so much again.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 6 de Mzo. de 2019

Comentada:

el 6 de Mzo. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by