how to draw real time plot in guide matlab

i am taking 3 input sensor values from the arduino. i want to plot these values in 3 different axes created in matlab guide.these plot should be in real time.how can i get the sensor data separetely.i treid to write code for single sensor here is the code
function varargout = vvx(varargin)
% VVX M-file for vvx.fig
% VVX, by itself, creates a new VVX or raises the existing
% singleton*.
%
% H = VVX returns the handle to a new VVX or the handle to
% the existing singleton*.
%
% VVX('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in VVX.M with the given input arguments.
%
% VVX('Property','Value',...) creates a new VVX or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before vvx_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to vvx_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help vvx
% Last Modified by GUIDE v2.5 02-Apr-2015 18:56:28
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @vvx_OpeningFcn, ...
'gui_OutputFcn', @vvx_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before vvx is made visible.
function vvx_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to vvx (see VARARGIN)
% Choose default command line output for vvx
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes vvx wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = vvx_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
% hObject handle to start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
obj = serial('COM17'); %creating the object
set(obj,'BaudRate',9600) %setting baudrate
fopen(obj); %open port
set(obj,'terminator','cr') %providing the terminator
pressure1=0; % a variable for y axis
time1=now; % time is in x axis
while 2>1
time2=now;
x=[time1 time2];
pressure2 = fscanf(obj) ;
pressure=[pressure1 pressure2];
if (pressure>200)&(pressure<500)
set(handles,'XData','g',[get(handles,'XData') x]);
set(handles,'YData','g',[get(handles,'YData') a]);
else if (pressure >300)&(pressure<700)
set(handles,'XData','o',[get(handles,'XData') x]);
set(handles,'YData','o',[get(handles,'YData') a])
else
set(handles,'XData','r',[get(handles,'XData') x]);
set(handles,'YData','r',[get(handles,'YData') a])
end
end
set(handles.uitable1, 'data', a2);
drawnow;
datetick('x','HH:MM') %change the axis
pause(0.5);
pressure1=pressure2;
time1=time2;
end

 Accepted Answer

raki - rather than using a while loop in your start button callback, consider using a timer to read data from Arduino. See an example at not able to read into MATLAB from serial Arduino using a timer to get an idea of how to do this.
As for your above code not doing what you expect, look closely at what you are trying to set with the new XData and YData
if (pressure>200)&(pressure<500)
set(handles,'XData','g',[get(handles,'XData') x]);
set(handles,'YData','g',[get(handles,'YData') a]);
else if (pressure >300)&(pressure<700)
set(handles,'XData','o',[get(handles,'XData') x]);
set(handles,'YData','o',[get(handles,'YData') a])
else
set(handles,'XData','r',[get(handles,'XData') x]);
set(handles,'YData','r',[get(handles,'YData') a])
end
end
The above code has too many ends (unless you are missing an if in there) so please remove the last one. And look at any of the
set(handles,...
handles is a structure of handles to the GUI controls (start, uitable1, axes1, axes2, axes3, etc.) and is not a handle to particular graphics object like a plot which is what I think you are intending it to be.
For example, you would need something like
% create a graphics object for a particular axes
handles.hPlot1 = plot(handles.axes1, NaN, NaN);
guidata(hObject, handles);
In the above we have created an "empty" plot on axes1, and then save the graphics handle to the handles structure (using guidata). Note that this code could be written in your GUI's opening function. Now, in the timer callback (or in what you have done above), you would do
if (pressure>200)&(pressure<500)
set(handles.hPlot1,'XData',[get(handles.hPlot1,'XData') x], ...
'YData',[get(handles.hPlot1,'YData') a], ...
'MarkerEdgeColor', 'g');
else if (pressure >300)&(pressure<700)
set(handles.hPlot1,'XData',[get(handles.hPlot1,'XData') x], ...
'YData',[get(handles.hPlot1,'YData') a], ...
'Marker','o');
else
set(handles.hPlot1,'XData',[get(handles.hPlot1,'XData') x], ...
'YData',[get(handles.hPlot1,'YData') a], …
'MarkerEdgeColor', 'r');
end
Note that your above code changes the current and all of the past pressure data which may not be desirable.
Note also, that you haven't defined a.

9 Comments

sir i modified the program and run it but i get an error
??? Error using ==> set
Invalid parameter/value pair arguments
Error in ==> ssssss>pushbutton1_Callback at 115
set(handles.hPlot1,'XData',[get(handles.hPlot1,'XData') x], ...
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> ssssss at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)ssssss('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
here is the code
function varargout = ssssss(varargin)
% SSSSSS M-file for ssssss.fig
% SSSSSS, by itself, creates a new SSSSSS or raises the existing
% singleton*.
%
% H = SSSSSS returns the handle to a new SSSSSS or the handle to
% the existing singleton*.
%
% SSSSSS('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SSSSSS.M with the given input arguments.
%
% SSSSSS('Property','Value',...) creates a new SSSSSS or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ssssss_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ssssss_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help ssssss
% Last Modified by GUIDE v2.5 05-Apr-2015 18:09:10
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ssssss_OpeningFcn, ...
'gui_OutputFcn', @ssssss_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before ssssss is made visible.
function ssssss_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to ssssss (see VARARGIN)
% Choose default command line output for ssssss
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes ssssss wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = ssssss_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- 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)
obj = serial('COM17'); %creating the object
set(obj,'BaudRate',9600) %setting baudrate
fopen(obj); %open port
set(obj,'terminator','cr') %providing the terminator
pressure1=0; % a variable for y axis
time1=now; % time is in x axis
% create a graphics object for a particular axes
handles.hPlot1 = plot(handles.axes1, NaN, NaN);
guidata(hObject, handles);
while 2>1 % infinite loop
time2=now;
x=[time1 time2];
ip_data = fscanf(obj) ;
pressure2= str2num(ip_data) ;
pressure=[pressure1 pressure2];
line(x,pressure);
if and(gt(pressure,200),lt(pressure,500))
set(handles.hPlot1,'XData',[get(handles.hPlot1,'XData') x], ...
handles.hPlot1,'YData',[get(handles.hPlot1,'YData') pressure], 'g');
else if and(gt(pressure ,300),lt(pressure,700))
set(handles.hPlot1,'XData',[get(handles.hPlot1,'XData') x], ...
handles.hPlot1,'YData',[get(handles.hPlot1,'YData') pressure], 'o');
else
set(handles.hPlot1,'XData',[get(handles.hPlot1,'XData') x], ...
handles.hPlot1,'YData',[get(handles.hPlot1,'YData') pressure], 'r');
end
datetick('x','HH:MM') %change the axis
pause(0.5);
pressure1=pressure2;
time1=time2;
end
end
I made a copy and paste mistake in the set. I've updated my answer with the correction (there is no need for the second handles.hPlot1).
sir now i run the code but for the given condition the color change not obtained in the real time graph
Raki - please explain what you mean by the color change not obtained in the real time grpah. Which colour do you see? Which colour are you expecting?
the plot obtained is blue in color for all range of the input. i want the color to be changed to green or orange or red depending upon the change in input as mentioned in the code. Can you help me for this?
Raki - if you are using the code above, then try replacing the 'MarkerEdgeColor' with just 'Color' to change the colours as
if (pressure>200)&(pressure<500)
set(handles.hPlot1,'XData',[get(handles.hPlot1,'XData') x], ...
'YData',[get(handles.hPlot1,'YData') a], ...
'Color', 'g');
else if (pressure >300)&(pressure<700)
set(handles.hPlot1,'XData',[get(handles.hPlot1,'XData') x], ...
'YData',[get(handles.hPlot1,'YData') a], ...
'Color',[1 0.5 0]);
else
set(handles.hPlot1,'XData',[get(handles.hPlot1,'XData') x], ...
'YData',[get(handles.hPlot1,'YData') a], …
'Color', 'r');
end
Note how [1 0.5 0] is used for orange ('o' corresponds to circles).
thank u sir
the code is work fine. sir i want to read both pressure and temperature from arduino to matlab,how to write code for this
Hello sir, I recently use this code to plot real time motion signals,
and i got this
here is the code:
if true
set(handles.hPlot1,'XData',[get(handles.hPlot1,'XData') time_x], ...
'YData',[get(handles.hPlot1,'YData') Low_Noise_Accelerometer_X], ...
'MarkerEdgeColor', 'm');
end
time_x and Low_Noise_Accelerometer_X are one-dimension arrays.
Could you possibly know why i get this result?
I suspect that your time_x data is not ordered correctly or there are duplicate values. Since this array corresponds to time, it should be monotonically increasing. What does it look like? Call
plot(time_x)
to find out.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 4 Apr 2015

Commented:

on 20 Mar 2017

Community Treasure Hunt

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

Start Hunting!