1. 程式人生 > 其它 >【物理應用】基於matlab計步【含Matlab原始碼 524期】

【物理應用】基於matlab計步【含Matlab原始碼 524期】

一、簡介

基於matlab計步

二、原始碼

%對原始觀測得到的資料進行獲取
clc, clear;
%加速度陣列
accspe_data = [];
time_record = [];
fid=fopen('sensor_normal.txt', 'r', 'n', 'utf-8');
%初始賦值使迴圈開始
tline = 1;  
%對文字進行迴圈讀入
%line = 'owieo';
while tline
    %fgetl獲取某一行
    tline=fgetl(fid);
    %轉換編碼形式
    tline = native2unicode(tline);
    %enc = detect_encoding(tline)
    %轉換為char型別
    line = char(tline); 
    %對值進行輸出
    %disp(line);
    %提取的資料
   % data = regexp(line, ':', 'split');
    %disp(line);
    %disp(length(line));
%     res = '';
% %     for len = 1:length(line)
% %         res = strcat(res,line(len));         %字串相加
% %         %disp('oiwei');
%     end
    
    data = strsplit(line, ':');
    %從字串中提取所需要的資料
    if strcmp(data(1), '加速度')
        volecity = data(2);
        vole = strsplit(char(volecity));
        %disp(vole);
        vole_x = str2double(vole(2));
        vole_y = str2double(vole(3));
        vole_z = str2double(vole(4));
        vole_res = sqrt(power(vole_x, 2) + power(vole_y, 2) + power(vole_z, 2));
        accspe_data = [accspe_data, vole_res];
    end
    
    if strcmp(data(1), '時間')
        time = strcat(data(2), data(3), data(4));
        time_data = strsplit(char(time));
        time_record = [time_record, (time_data(3))];
    end
   % end
    %print('oiweoi')
    %print(class(tline));
    %提取其中的資料 
end
%判斷是否缺少資料
len = length(time_record);
time_check = ones(1, len);
for num = 1:len
    time_num = str2double(char(time_record(num)));
    
    hour = floor(time_num / 10000);
    minute = floor((time_num - hour * 10000) / 100);
    second = floor((time_num - hour * 10000 - minute * 100));
    
    %將時間轉換為秒
    time_check(num) = hour * 3600 + minute * 60 + second;
    if num >= 2 && (time_check(num) - time_check(num - 1)) > 1
        disp('資料採集有誤,部分資料缺失!!!');
        disp(time_check(num) - time_check(num - 1)) ;
    end
end
fclose(fid);

三、執行結果

四、備註

版本:2014a
完整程式碼或代寫加1564658423