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 ratedt = 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 datafor i = 1:rowresult(i,1) = sqrt(mean(data(:,i).^2)); % rmsresult(i,2) = std(data(:,i)); % stdif (flage.mean)result(i,3) = mean(abs(data(:,i))); % meanresult(i,4) = max(abs(data(:,i))); % maxresult(i,5) = min(abs(data(:,i))); % minresult(i,6) = median(abs(data(:,i))); % medianelseresult(i,3) = mean(data(:,i));result(i,4) = max(data(:,i)); % maxresult(i,5) = min(data(:,i)); % minresult(i,6) = median(data(:,i)); % medianendendprecision = 3;result = round(result,3);vnames = {'rms','std','mean','max','min','median'};result = array2table(result,'VariableNames',vnames);end
3. 画图函数
function setting_figure()
% Drawing Parameter Settingsgrid on;set(gca,'ygrid','on','gridlinestyle','--','Gridalpha',0.5,'linewidth',2);set(gcf,'color','w');set(gca,'Fontsize',25);print(gcf,'-clipboard','-dbitmap');
end