在matlab中将输出数据写入excel文件

时间:2013-03-02 17:28:02

标签: matlab

我有50张图片,我想在matlab中为每个图像列写入数据到文本文件,但是当我运行程序时,它只会写入一个图像值。

srcFiles = dir('E:\practice \\*.jpg');
for i = 1 : length(srcFiles)
  filename = strcat('E:\practice\',srcFiles(i).name);
  I = imread(filename);
  B=bwlabel(I);
  D=regionprops(B,'MajorAxisLength','MinorAxisLength','Extent');
  j=D.MajorAxisLength;
  k=D.MinorAxisLength;
  Axialratio=j/k
  ex=[D.Extent]
  fid=fopen('mu.txt ','wt+');
  fprintf(fid,'extent  Axialratio\n');
  fprintf(fid,'%6f     %6f \n',[ex,Axialratio]);
  fclose(fid);
end 

1 个答案:

答案 0 :(得分:0)

您正在使用fopen选项进行wt+。首先,没有这样的选择。也许你的意思是w+。文档说:

Open or create new file for reading and writing. Discard existing contents, if any.

因此,每次为每张图片进入for循环时,您都会丢弃内容。执行以下操作:

srcFiles = dir('E:\practice \\*.jpg');
fid=fopen('mu.txt ','w+');
for i = 1 : length(srcFiles)
  filename = strcat('E:\practice\',srcFiles(i).name);
  I = imread(filename);
  B=bwlabel(I);
  D=regionprops(B,'MajorAxisLength','MinorAxisLength','Extent');
  j=D.MajorAxisLength;
  k=D.MinorAxisLength;
  Axialratio=j/k
  ex=[D.Extent]

  fprintf(fid,'extent  Axialratio\n');
  fprintf(fid,'%6f     %6f \n',ex,Axialratio); //Also don't create a matrix, you could have written it as two separate quantities as I have done now.
 end 
fclose(fid);

这应该有用。