Bootstrap

Matlab学习-自定义函数

Matlab学习-自定义函数

常用自定义函数

1. 打印时间

function result = calculate_time(time)
    % Function describe : calculate time
    % Input  : time:N*1
    % Output : result.hour/min/sec = hour/min/sec
    %                   result.fre = Sampling rate
    dt = time(2) - time(1);
    freq = 1/dt;
    result.fre = freq;

    total_time = size(time,1)*dt;
    hour = floor(total_time/3600);
    min = floor(mod(total_time,3600)/60);
    sec = total_time-3600*hour-60*min;

    result.hour = hour;
    result.min = min;
    result.sec = sec;

    s = sprintf('Time length: %dh-%dm-%.2fs, time diff: %f s, frequency: %d Hz\n', hour, min, sec, dt, round(freq));
    disp(s);

end

在这里插入图片描述

2. 计算统计参数

function result = statistical_analysis(data,flag,precision)
% Input : data: analyser data
%         flag: true or false   analysis absolute data or raw data
%         precision: output data precision
% Output: result = [rms std mean max min median]

column = size(data,2);
flage.absolute = flag;  % true: abs     flase: raw data

for i = 1:column
    result(i,1) = sqrt(mean(data(:,i).^2));     % rms
    result(i,2) = std(data(:,i));               % std
    if (flage.absolute)
        result(i,3) = mean(abs(data(:,i)));     % mean
        result(i,4) = max(abs(data(:,i)));      % max
        result(i,5) = min(abs(data(:,i)));      % min
        result(i,6) = median(abs(data(:,i)));   % median
    else
        result(i,3) = mean(data(:,i));          % mean
        result(i,4) = max(data(:,i));           % max
        result(i,5) = min(data(:,i));           % min
        result(i,6) = median(data(:,i));        % median
    end
end

result = round(result,precision);

vnames = {'rms','std','mean','max','min','median'};
result = array2table(result,'VariableNames',vnames);

end


在这里插入图片描述

3. 画图函数

function setting_figure()
% Drawing Parameter Settings
    grid on;
    set(gca,'ygrid','on','gridlinestyle','--','Gridalpha',0.5,'linewidth',2);
    set(gcf,'color','w');
    set(gca,'Fontsize',25);
    print(gcf,'-clipboard','-dbitmap');
end

4. 存储kml数据

function save_file_kml(data,file_path)
% Description: save kml file
% Input: data = [time latitude(deg) longitude(deg)];
%        file_path : save kml file name and path

vname = {'time','latitude','longitude'};
data = array2table(data,'VariableNames',vname);
lat = data.latitude;
lon = data.longitude;

kmlname = fullfile(file_path);
kmlname_line = strcat(kmlname,'_line.kml');
kmlname_point = strcat(kmlname,'_point.kml');

kmlwriteline(kmlname_line,lat,lon);
kmlwritepoint(kmlname_point,lat,lon);

end

5. 存储CSV文件

function save_csv(A, path)
% Description: save data
% Input : A: save data
%         path: save path
[row, col] = size(A);
fid = fopen(path, 'w');

for i=1:row
    for j=1:col-1
        fprintf(fid, '%.9f,', A(i,j));
    end
    fprintf(fid, '%.9f\r\n', A(i,col));
end
fclose(fid);

end



6. 批量读取文件名

批量读取指定路径下指定类型文件

function filelist = extract_file(fpath,file_type)
% Descration: extract the names of all files in the floder
% Input: fpath: path of floder
%        file_type: file type
% Output: filelist: file name

filelist = dir(fullfile(fpath,file_type));
filelist = {filelist.name}';
end

在这里插入图片描述

;