matlab如何批量读取文件

此方法无需重命名文件,只需要把文件放在同一文件夹了即可。其方法是利用dir把目录下的的文件名保存的一个数组中,然后挨个调用就可以了。

clc;clear
addpath('shuju')
n=166;
I=cell(1,n);
file_path ='F:\zdmatlab\data\data1\Subj01\SAG_T1_FSPGR_BRAVO_3\shuju';% 图像文件夹路径  
img_path_list=dir(strcat(file_path,''));%获取该文件夹中所有格式的图像 
img_num = length(img_path_list);%获取图像总数量 
if img_num > 0 %有满足条件的图像  
      for j = 3:img_num%逐一读取图像  
          image_name = img_path_list(j).name;% 图像名  
          info = dicominfo( image_name);
           image = dicomread(info);
          I{j-2}=image;
      end 
end 
figure;
for i=1:166
a=double(cell2mat(I(i)));
imshow(a,[])
end

这个程序的功能是批量读取shuju文件夹下的.dicom文件,并逐个imshow,可以看到一个漂亮的动图。
这里只能放一张,没有动态效果

猜你喜欢

转载自blog.csdn.net/qq_27273607/article/details/83691952