欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【代码】Matlab 读写文件

程序员文章站 2022-04-01 08:38:45
...

这里以wav文件为例,自动化读取和写入文件代码:

1、循环加载某一格式的数据,例如.wav文件

1、循环加载某一格式的数据,例如.wav文件

folder = pwd;
folder_data = [folder '\ari0_1209\'];
folder_save = [folder '\e0\'];
files = dir([folder_data '/*.mat']);
N = length(files);      % the number of the files

for m= 1:N
    file = [folder_data files(m).name];
    % res.file = file;
    load(file);
end


2、循环存储文件

chr = ['subject',num2str(i),'.mat'];
save(chr,'hl','hr');

格式化与指定文件夹输出
chr = ['subject',num2str(m,'%03d'),'.mat'];
save([folder_save chr],'hl','hr');

3、返回上层文件夹
	% get the father folder
	str = pwd;
	index_dir = strfind(str,'\');
	str_temp = str(1:index_dir(end)-1);
	
4、读子文件夹
	
	folder_data = [str_temp '\standard_hrir_database'];
	subdir = dir( folder_data);    
	
	if( isequal( subdir( index ).name, '.' )||…
	        isequal( subdir( index ).name, '..')||...
	        ~subdir( index ).isdir)               % 如果不是目录则跳过
	        continue;
	    end
	    subject_num = subject_num+1;
	    subdirpath = fullfile( folder_data, subdir( index ).name, '*.mat' );
	
	    mat = dir( subdirpath );
	    datpath = fullfile( folder_data, subdir( index ).name, mat.name);
	    load( datpath );
	

for m= 1:N

    file = [folder_data files(m).name];

    % res.file = file;

    load(file);

end

 

 

2、循环存储文件

 

chr = ['subject',num2str(i),'.mat'];

save(chr,'hl','hr');

 

格式化与指定文件夹输出

chr = ['subject',num2str(m,'%03d'),'.mat'];

save([folder_save chr],'hl','hr');

 

3、返回上层文件夹

% get the father folder

str = pwd;

index_dir = strfind(str,'\');

str_temp = str(1:index_dir(end)-1);

 

4、读子文件夹

 

folder_data = [str_temp '\standard_hrir_database'];

subdir = dir( folder_data);   

 

if( isequal( subdir( index ).name, '.' )||…

        isequal( subdir( index ).name, '..')||...

        ~subdir( index ).isdir)               % 如果不是目录则跳过

        continue;

    end

    subject_num = subject_num+1;

    subdirpath = fullfile( folder_data, subdir( index ).name, '*.mat' );

 

    mat = dir( subdirpath );

    datpath = fullfile( folder_data, subdir( index ).name, mat.name);

    load( datpath );