Matlab與C實時聯合模擬二

lc阿振發表於2017-09-28

方式
C資料實時寫入文字c.txt
Matlab實時讀取c.txt資料

matlab程式部分

clc
clear all
close all


figure(1)
%座標軸範圍
axis([-10 10 -10 10]);
plot([0 10],[0,10],'b-');
xlabel('x');
ylabel('y');
grid on
hold on 
label=0;
stop=0;
%實現資料在畫圖中實時顯示,動態更新
while ~stop
    data=textread('E:\work\workspace\c.txt');
    shape=size(data);
    len=shape(1);
    %檢測是否有新資料
    if len>label
        axis([-10 10 -10 10]);
        %將更新的資料動態顯示
        plot(data(1:len,1),data(1:len,1),'r-');
        grid on
        hold on 
        drawnow;
        %記錄資料長度
        label=len;
        %終止條件
        if data(len,1)>10
            stop=1;
        end

    end



end

結果如下:資料來自於C程式,MATLAB做實時動態顯示

圖1

圖2

相關文章