Matlab学习-自定义函数
常用自定义函数
文章目录
- Matlab学习-自定义函数
- 1. 打印时间
- 2. 计算统计参数
- 3. 画图函数
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)
% Input : data: analyser data
% Output: result = [rms std mean max min median]
row = size(data,2);
flage.mean = false; % true: abs flase: raw data
for i = 1:row
result(i,1) = sqrt(mean(data(:,i).^2)); % rms
result(i,2) = std(data(:,i)); % std
if (flage.mean)
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));
result(i,4) = max(data(:,i)); % max
result(i,5) = min(data(:,i)); % min
result(i,6) = median(data(:,i)); % median
end
end
precision = 3;
result = round(result,3);
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