How to save each data from for loop
Mostrar comentarios más antiguos
I have a for loop(last for loop), but every character overwrites and I have only the final data left.
[L Ne]=bwlabel(imagen);
%%Measure properties of image regions
propied=regionprops(L,'BoundingBox');
hold on
%%Plot Bounding Box
for n=1:size(propied,1)
rectangle('Position',propied(n).BoundingBox,'EdgeColor','g','LineWidth',2)
end
hold off
pause (1)
%%Objects extraction
figure(3)
for n=1:Ne
[r,c] = find(L==n);
n1=imagen(min(r):max(r),min(c):max(c));
%imshow(~n1);
pause(0.5)
end
2 comentarios
Ameer Hamza
el 30 de Abr. de 2018
What is imagen()? Is it your own function. In any case refer to my answer below.
neha gautam
el 30 de Abr. de 2018
Respuesta aceptada
Más respuestas (1)
Ameer Hamza
el 30 de Abr. de 2018
Editada: Ameer Hamza
el 30 de Abr. de 2018
Change your last for loop as follow
n1 = []
for n=1:Ne
[r,c] = find(L==n);
n1 = [n1 imagen(min(r):max(r),min(c):max(c))];
%imshow(~n1);
pause(0.5)
end
n1 will save all the values.
8 comentarios
No. Do NOT augment the array dynamically plus that will string all the separate subimage regions together into one array even if the code would run (which it won't because unless is extremely rare case the various sub-images won't all be the same size so can't concatenate).
Ameer Hamza
el 30 de Abr. de 2018
I initially thought that imagen is a user-defined function which returns character as OP also mentioned about characters in the first line of the question. Now I realized that it is a matrix, so it will cause problems on the concatenation.
@neha, In this case, the cell array is the best option as given by @dbp's answer.
dpb
el 30 de Abr. de 2018
Even if were so, except in very rare instances, don't use the dynamic augmentation; it kills performance and is bad habit to teach newbies.
neha gautam
el 30 de Abr. de 2018
Ameer Hamza
el 30 de Abr. de 2018
@dpb thanks for raising a good point.
@neha, Why do you want to save all matrices separately? That's the whole point of having a cell array so that data should be kept organized. You will have to name files dynamically and it will also cause issues when next time you try to load data. See here why is it bad: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
Just save complete cell array using
save('data.mat', 'n1'); % second argument is variable name.
"Now, I have a cell 1*Ne..."
If you used my code you will have a Ne-length column vector, not a row which might indicate you used the dynamic augmentation. The orientation doesn't really matter; not preallocating and indexing into the array is a really bad habit to get into; I can't strongly-enough recommend to fix that if is what have done. The ML editor will have flagged the code lines in the RH margin if so...
Ameer is correct in pointing another just-as-bad idea in his recommendation to just use the array; as the link notes you'll create far more issues than solutions otherwise -- in the words of the old maps pointing to terra incognita, "There be dragons!"
neha gautam
el 1 de Mayo de 2018
dpb
el 1 de Mayo de 2018
You have my permission to do so, yes... :)
Good plan to follow "best practices" in avoiding these kinds of what_seem_so_easy_to_do techniques, but the short-term gratification leads to longer-term misery when get to anything but the most trivial of applictions--best to learn to avoid altogether while learning; then with time one can recognize when it is time to break the rule for a specific purpose (altho that's not that common, it does occur).
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!