Need help with GUI axes and arduino

4 Ansichten (letzte 30 Tage)
Viet Le
Viet Le am 27 Okt. 2016
Beantwortet: Viet Le am 27 Okt. 2016
So I'm planning on making an Matlab interface with Arduino UnoR3 and got confused with the axes (graph as you may). I referred some sources and got this code going as wanted. However, I wonder if I should put this on "push_button_callback" or somewhere else (if so, where should I put it); further, as the plot scrolling, I find that the previous part of the graph is lost, how can I recall that part, or I should just plot another figure manually?
P/s: this is just a demo with assigned data
Many thanks
clear;
clc;
%User Defined Properties
plotTitle = 'Serial Data Log'; % plot title
xLabel = 'Elapsed Time (s)'; % x-axis label
yLabel = 'Data'; % y-axis label
plotGrid = 'on'; % 'off' to turn off grid
min = 0; % set y-min
max = 150; % set y-max
scrollWidth = 50; % display period in plot, plot entire data log if <= 0
delay = .5; % make sure sample faster than resolution
dat=randi(100,1,200);
%Define Function Variables
time = 0;
data = 0;
i=0;
%Set up Plot
plotGraph = plot(time,data);
title(plotTitle,'FontSize',25);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
axis([0 10 min max]);
grid(plotGrid);
tic
while ((ishandle(plotGraph) && i<length(dat)))%Loop when Plot is Active
if(~isempty(dat) && isfloat(dat)) %Make sure Data Type is Correct
i = i + 1;
time(i) = i*0.5; %Extract Elapsed Time
data(i) = dat(i); %Extract 1st Data Element
%Set Axis according to Scroll Width
if(scrollWidth > 0)
set(plotGraph,'XData',time(time > time(i)-scrollWidth),'YData',data(time > time(i)-scrollWidth));
%plot(time(time > time(count)-scrollWidth),data3(time > time(count)-scrollWidth));
axis([time(i)-scrollWidth time(i) min max]);
%set(plotGraph,'XData',time(time > time(count)-scrollWidth),'YData',data3(time > time(count)-scrollWidth));
%axis([time(count)-scrollWidth time(count) min max]);
else
set(plotGraph,'XData',time,'YData',data);
axis([0 time(i) min max]);
end
%Allow MATLAB to Update Plot
pause(delay);
end
end
%Close Serial COM Port and Delete useless Variables
clear count dat delay max min baudRate plotGraph plotGrid plotTitle s ...
scrollWidth serialPort xLabel yLabel;
end

Antworten (1)

Viet Le
Viet Le am 27 Okt. 2016
Update, I have modified some lines to suit my purpose. However, there are lines confused me.
I.e if I do it like below, the code runs smoothly
if(i<(scrollWidth/delay))
set(plotGraph,'XData',time,'YData',data);
axis([0 scrollWidth min max]);
else
set(plotGraph,'XData',time,'YData',data);
axis([time(i)-scrollWidth time(i) min max]);
end
If I do it like this
if(i<(scrollWidth/delay))
set(plotGraph,'XData',time,'YData',data);
axis([0 scrollWidth min max]);
else
set(plotGraph,'XData',time,'YData',data);
axis([time(i -(scrollWidth/delay)) time(i) min max]);
%time(i-(scrollWidth/delay)) = time(i)-delay
end
then the code stop at i = scrollWidth/delay (in this case, i=100). Can someone explain my problem? I suppose Matlab can access previous values of time(i) while in a loop, but it doesnt make sense to me. Many thanks

Kategorien

Mehr zu MATLAB Support Package for Arduino Hardware finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by