MATLAB: How to draw real time plot in guide matlab

arduinoguireal time

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

Best 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.
Related Question