Save unique variable after same routine of n loops and output to a table or file?
2 views (last 30 days)
Show older comments
Svet Shkolyar
on 27 Aug 2015
Commented: Walter Roberson
on 28 Aug 2015
I have a code that takes a .jpg file and performs some pixel analysis functions to it and outputs a floating variable. I want to save this unique variable after looping the routine for n .jpg files in a directory. Then I want to tabulate all of these variables and write them to a file.
Why does the below not allow me to do it?
jpegFiles = fullfile('Users/myfolder/Desktop/thresholding/*.jpg');
for k = 1:length(jpegFiles)
filename = strcat(num2str(k), '.jpg');
if exist(filename, 'file');
fprintf('%s successfully read.\n', filename);
a = imread(filename, 'jpg');
% Calculation functions omitted for brevity
Cov = somefunction;
fprintf('\n%s%.2f\n','Image pixel coverage:',Cov);
%why won't this let me store Cov variables as a matrix? It overrides the previous Covs if I try the below.
for i = 1:k
Cov2 = num2str(Cov); %create string from number
fprintf('The coverage as a string is:',Cov2);
end
%This line forward prints only the last value to file in same dir as image files but I want it to do so for all Covs:
fid = fopen('Output.csv','wt');
fprintf(fid,'%0.4f\n',Cov);
fclose(fid);
end
0 Comments
Accepted Answer
Walter Roberson
on 28 Aug 2015
You have
for k = 1:length(jpegFiles)
so your "k" is a scalar.
You have
for M = 1:length(k);
Because k is a scalar, length(k) is 1, so you are effectively using
for M = 1:1
The natural immediate reading would be to think that you wanted
for M = 1 : k
but if you do that then you are going to be writing the same unchanged COV matrix to every element of A, since COV does not change in you "for M" loop. This suggest that a "for M" loop is not desirable at that point in the code.
You have
dlmwrite('kerogen coverage.xls',A)
As per the above you only ever write to A{1}, so you will only ever have one output set in the file with your code. You are also always writing to the same file which would keep overwriting that one output set with a new value.
I suspect that what you want is to change
for M = 1:length(k);
A{M} = Cov;
fprintf('\n%s%.2f\n','This value is stored in the percentage coverage matrix:',A{M});
celldisp(A);
end
to
A{k} = Cov;
fprintf('\n%s%.2f\n','This value is stored in the percentage coverage matrix:',A{k});
celldisp(A);
In this way A is going to get larger on each k iteration, and will always include the previous output, so although you overwrite the same file each time, the file will get updated as the run progresses.
You could have a problem if the dlmwrite() fails as you could end up scribbling over everything you have done so far. That is why it is not a good idea to continually write over all of a file.
2 Comments
More Answers (1)
Walter Roberson
on 27 Aug 2015
It does not allow you to do that as a deliberate design decision to reduce the number of times that people use this kind of bad programming technique. Dynamic variable names almost always cause problems and should be avoided whenever possible.
4 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!