1. 文件操作类
1.1. 添加某路径下的所有文件到搜索路径
addpath( genpath ( '..\folder ' ) );
1.2. 获取某路径source_path下包含关键字keyword的扩展名为.xxx的所有文件
file_list = dir( [ source_path '*key_word_1**key_word_2*.xxx']);
1.3. 递归查找文件夹下的某类型文件
filePath = './240625/**/';
%filePath = './240625/*/qcom/**/'; % 遍历某些文件夹中的文件
file_list = dir( [ filePath '*ScreenOn_imu_data*.csv']);
1.4. 读取hdf5文件
hinfo = hdf5info('data.hdf5');
filename='data.hdf5';
attr = hdf5read(hinfo.GroupHierarchy.Groups(1).Datasets(3));//举个例子
1.5. 格式化读入txt文件(速度比load快)
str=[];
for i=1:41
str= [str '%f,'];
end
str= [str '%f'];
fid = fopen('xxx.txt');
C = textscan(fid,str,'EndOfLine','\r\n');//换行符号为‘\r\n’
fclose(fid);
1.6. 删除文件
删除一个文件:delete(filename)
删除一类文件:delete(‘*.后缀’)
删除文件夹及文件夹下的文件:rmdir(文件夹名字,‘s)
1.7 按行读取txt文件
fid = fopen('result_all.txt','rt');
while feof(fid) ~=1
proc_times = proc_times +1;
line = fgetl(fid);
end
end
fclose(fid);
1.8 保存矩阵到表格文件
file_name = strrep(IMUFile,'.csv','');
saveFilePathName = fullfile(resPath, strcat(file_name, '_newResult.csv'));
T = array2table(saveData);
T.Properties.VariableNames(1:2) = {'timestamp(s)','label'};
writetable(T,saveFilePathName);
2.绘图类
2.1. 绘制透明底色图
figure;
plot(sin(0:0.1:pi));
axis off;% 去掉坐标轴
set(gcf,'color','none');
set(gca,'color','none');
% but this following little detail took me ages to figure out
set(gcf,'InvertHardCopy','off');
2.2. 绘制不规则子图
例如绘制左1,右二的子图,可以用:
figure;
subplot(1,2,1)
bar( randn(2) );
subplot(2,2,2)
plot( sin(0:0.01:pi) );
subplot(2,2,4)
plot( randn(4) );
2.3. 修改绘图窗口位置和大小
figure('name',strrep(str1,'_','\_'));
set(gcf,'unit','normalized','position',[0.1,0.2,0.7,0.6]);%figture位置,最下角,宽高
2.4. 绘制CDF累计分布函数
figure;
h1=cdfplot(sdf);//主要
set(h1,'color','b','LineWidth',3);
set(gca,'FontSize',15);
xlabel('Position Error','FontSize',20);
ylabel('CDF','FontSize',20);
title('')
2.5. plot绘图坐标轴,图例,字体,线型号等
x=0:pi/100:pi;
y=sin(x);
plot(x,y);
2.6. plot绘图后关闭,保存到指定位置path并以file_name.fig保存
h=figure('name',file_name);
plot(accData);
saveas(h,[path file_name '.fig']);
close(gcf);
3. 杂项
3.1.格式化输出
fprintf(' rmse total distance is %f \n', error );
3.2. 运行时间计时
tic;
toc;
3.3. 字符串操作
3.3.1. 字符串替换
b=strrep(a,'is','')
3.4. 将纬度经度高度写成KML文件函数
kmlwriteline(filename,latitude,longitude)
kmlwriteline(filename,latitude,longitude,altitude)
kmlwriteline(___,Name,Value)
3.5 smoothdata的滑动窗口规律
3.6 matlab启动太慢的解决
添加系统环境变量,直接配置matlab的许可证书,节省搜索时间。
https://blog.csdn.net/Jeffxu_lib/article/details/84110456