我有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
答案 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);
这应该有用。