Matlab实时绘制串口的多个数据

时间:2014-07-23 12:36:08

标签: matlab plot serial-port arduino

-->

我的应用程序通过ARDUINO UNO平台从传感器读取数据,然后通过串行端口我设法读取MATLAB中需要的所有数据。所以我现在有3个数据要实时绘制(数据,数据2,数据3)在相同的图形上

我还设法使用我在mathworks上找到的一些代码一次绘制一个数据,并对其进行了一些修改,这不适合我的项目。

以下是我用来绘制其中一个数据的matlab代码:

clear
clc

%User Defined Properties 
serialPort = 'COM7';           % define COM port #
baudeRate = 115200;
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 = -200;                     % set y-min
max = 200;                      % set y-max
scrollWidth = 10;               % display period in plot, plot entire data log if <= 0
delay = .01;                    % make sure sample faster than resolution

%Define Function Variables
time = 0;
data = 0;
data2 = 0;
data3 = 0;
count = 0;

%Set up Plot
plotGraph = plot(time,data,time,data2,time,data3);

title(plotTitle,'FontSize',25);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
axis([0 10 min max]);
grid(plotGrid);

%Open Serial COM Port
s = serial(serialPort, 'BaudRate',baudeRate)
disp('Close Plot to End Session');
fopen(s);

tic

while ishandle(plotGraph) %Loop when Plot is Active

    dat = fscanf(s,'%f'); %Read Data from Serial as Float

    if(~isempty(dat) && isfloat(dat)) %Make sure Data Type is Correct        
        count = count + 1;    
        time(count) = toc;    %Extract Elapsed Time
        data(count) = dat(1); %Extract 1st Data Element  
        data2(count) = dat(2);
        data3(count) = dat(3);

        data(count);
        data2(count);
        data3(count);

        %Set Axis according to Scroll Width
        if(scrollWidth > 0)
        set(plotGraph,'XData',time(time > time(count)-scrollWidth),'YData',data3(time > time(count)-scrollWidth));
        %plot(time(time > time(count)-scrollWidth),data3(time > time(count)-scrollWidth));
        axis([time(count)-scrollWidth time(count) 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(count) min max]);
        end

        %Allow MATLAB to Update Plot
        pause(delay);
    end
end

%Close Serial COM Port and Delete useless Variables
fclose(s);
clear count dat delay max min baudRate plotGraph plotGrid plotTitle s ...
        scrollWidth serialPort xLabel yLabel;


disp('Session Terminated...');

我需要在图形上用不同的颜色绘制所有3个数据(data,data2,data3)。请帮帮我。

3 个答案:

答案 0 :(得分:2)

我实际上使用了与您的某个项目相同的源代码。如果你仍然需要帮助,这里是我用来从加速度计绘制三条数据线的代码的修改版本。它描绘得很漂亮。但是,由于fscanf()所需的时间,你不能实现比每50ms左右更快的实时获取和绘制信号而没有混叠。

clear
clc

%User Defined Properties 
serialPort = 'COM7';            % define COM port #
plotTitle = 'Serial Data Log';  % plot title
xLabel = 'Elapsed Time (s)';    % x-axis label
yLabel = 'Acceleration';                % y-axis label
plotGrid = 'on';                % 'off' to turn off grid
min = -1.5;                     % set y-min
max = 2.5;                      % set y-max
scrollWidth = 10;               % display period in plot, plot entire data log if <= 0
delay = .0000001;                    % make sure sample faster than resolution

%Define Function Variables
time = 0;
data = zeros(3,1);
count = 0;

%Set up Plot
plotGraph = plot(time,data(1,:),'-r',...
            'LineWidth',2,...
            'MarkerFaceColor','w',...
            'MarkerSize',2);
hold on
plotGraph1 = plot(time,data(2,:),'-m',...
            'LineWidth',1,...
            'MarkerFaceColor','w',...
            'MarkerSize',2);
hold on
plotGraph2 = plot(time,data(3,:),'-b',...
            'LineWidth',1,...
            'MarkerFaceColor','w',...
            'MarkerSize',2);

title(plotTitle,'FontSize',25);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
axis([0 10 min max]);
grid(plotGrid);

%Open Serial COM Port
s = serial(serialPort, 'BaudRate', 115200)
disp('Close Plot to End Session');
fopen(s);

tic

while ishandle(plotGraph) && ishandle(plotGraph2) && ishandle(plotGraph1)  %Loop when Plot is Active

dat = fscanf(s,'%f'); %Read Data from Serial as Float

if(~isempty(dat) && isfloat(dat)) %Make sure Data Type is Correct        
    count = count + 1;    
    time(count) = toc;    %Extract Elapsed Time in seconds
    data(:,count) = dat(:,1); %Extract 1st Data Element         

    %Set Axis according to Scroll Width
    if(scrollWidth > 0)
    set(plotGraph,'XData',time(time > time(count)-scrollWidth),...
        'YData', data(3,time > time(count)-scrollWidth));
    set(plotGraph1,'XData',time(time > time(count)-scrollWidth),...
        'YData', data(2,time > time(count)-scrollWidth));
    set(plotGraph2,'XData',time(time > time(count)-scrollWidth),...
        'YData', data(1,time > time(count)-scrollWidth));

    axis([time(count)-scrollWidth time(count) min max]);
    else
    set(plotGraph,'XData',time,'YData',data(3,:));
    set(plotGraph1,'XData',time,'YData',data(2,:));
    set(plotGraph2,'XData',time,'YData',data(1,:));

    axis([0 time(count) min max]);
    end

    %Allow MATLAB to Update Plot
    pause(delay);
end
end

%Close Serial COM Port and Delete useless Variables
fclose(s);

clear count dat delay max min plotGraph plotGraph1 plotGraph2 plotGrid...
    plotTitle s scrollWidth serialPort xLabel yLabel;

disp('Session Terminated');

prompt = 'Export Data? [Y/N]: ';
str = input(prompt,'s');
if str == 'Y' || strcmp(str, ' Y') || str == 'y' || strcmp(str, ' y')
    %export data
    csvwrite('accelData.txt',data);
    type accelData.txt;
else
end

clear str prompt;

答案 1 :(得分:0)

使用line功能在同一个地块上再创建一条线 来自Help-&gt; MatLab-&gt; Graphics-&gt; Handle Graphics Objects-&gt; High-Level与Low-Level

  

如果再次调用线函数,MATLAB将在当前轴中绘制第二行而不删除第一行。

答案 2 :(得分:0)

只需使用以下内容:

Import-Csv -Path ".\test.csv" | 
    ForEach-Object {Add-Member -InputObject $_ -NotePropertyName 'managerUserName' -NotePropertyValue (Get-ADUser -Filter "employeeNumber -eq $($_.'Manager User Sys ID')").samaccountname -PassThru} |
    Export-Csv -Path ".\simpletest.csv" -NoTypeInformation
相关问题
最新问题