Problem plotting data in matlab GUI

Im taking data from arduino. Im using a button to start the data acquisition from arduino to matlab. This is the code.
function Send_Callback(hObject, eventdata, handles)
global tmax
delete(instrfind({'Port'},{'COM2'}));
s = serial('COM2','BaudRate',9600,'Terminator','CR/LF');
warning('off','MATLAB:serial:fscanf:unsuccessfulRead');
%Open port
fopen(s);
tmax = str2double(get(handles.edit2,'string'));
axes(handles.axes1);
axis([0 tmax 0 5.1] )
axes(handles.axes2);
axis([0 tmax 0 5.1] )
rate = 33;
i = 2;
t = 0;
% ejecutar bucle cronometrado
tic
AccelerationX = zeros(1,tmax*rate);
AccelerationY = zeros(1,tmax*rate);
while t<tmax
tic
t = toc;
a = fscanf(s,'%f,%f')';
AccelerationX(i)=a(1);
AccelerationY(i)=a(2);
% dibujar en la figura
x = linspace(0,i/rate,i);
set(handles.axes1,AccelerationX(1:i),x);
set(handles.axes2,AccelerationY(1:i),x);
drawnow
% seguir
i = i+1;
end
But i got this error when im trying to plot data.
Error using matlab.graphics.axis.Axes/set Invalid parameter/value pair arguments.
Error in ArduinoUnity3d>Send_Callback (line 155) set(handles.axes1,AccelerationX(1:i),x);
Error in gui_mainfcn (line 95) feval(varargin{:});
Error in ArduinoUnity3d (line 42) gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)ArduinoUnity3d('Send_Callback',hObject,eventdata,guidata(hObject)) Error while evaluating UIControl Callback
thanks

 Accepted Answer

Jan
Jan on 22 May 2017
Edited: Jan on 22 May 2017
handles.axes1 is the handle of an axes object. If you want to set one of its properties, you have to specify the name of the property. I cannot guess which axes' properties you want to set to the values AccelerationX(1:i) and x, but I'm in doubt that this is meant at all. Unfortunately you did not explain here or in a comment in the code, what the concerned command should do. But I guess boldly, that you want to draw a signal. Correct? Then you need a plot or line command at first:
lineH = line('XData', [], 'YData', [], 'Parent', handles.axes1); % Before WHILE
...
set(lineH, 'XData', AccelerationX(1:i), 'YData', x);
I do not know, if this is what you are looking for, but perhaps it shows you how to use the set command.
Note: This is not efficient:
axes(handles.axes1);
axis([0 tmax 0 5.1] )
Instead of making the axes the current object, better access the handle directly:
set(handles.axes1, 'XLim', [0, tmax], 'YLim', [0. 5.1]);
This is meaningless also:
tic
t = toc;
You measure the time between these two lines. The WHILE loop will run forever then. Omit the tic or even better use clock or now to determine the current time.
General hint: When you have problems with a command, the first thing you have to do is reading the documentation:
doc set
If this does not help and you want to ask the forum, care for explaining, what the failing line should do. It is hard to guess the purpose of failing code.

1 Comment

Thanks Sir, the code is working. i will follow your recommendations

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Tags

Asked:

on 22 May 2017

Commented:

on 22 May 2017

Community Treasure Hunt

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

Start Hunting!