looping variable with appended numbers

1 vue (au cours des 30 derniers jours)
Shane
Shane le 16 Déc 2014
Modifié(e) : Adam le 16 Déc 2014
I have an image that I want to loop through and give each iteration an extension such as imgVNIR1, imgVNIR2,...imgVNIRn, the same applies to the imgSWIR. the count will be dependent on the number of targets in the image
prompt = 'Enter number of object ';
result = input(prompt);
this is the code I have.(things to know: The full image is 320x768x361, img is the image in which a object is selected. the image is composed of 2 sensors side by side, VNIR and SWIR. Bands 125-130 are not used)
img = imrotate(im,90, 'nearest');
%[m,n]=size(img(:,:,124));
figure; imshow(img(:,:,124));
[~ , rect]=imcrop;
rect = floor(rect);
rowstart = rect(2);
rowend = rowstart+rect(4);
colstart = rect(1);
colend = colstart+rect(3);
imgVNIR = img(rowstart:rowend,colstart:colend,115);
imgSWIR = img(rowstart:rowend,colstart:colend,138);
The goal is to select an object to crop based on the number stored in result and have 2 outputs such that my work space should look like this if i had 3 objects
  • imgVNIR1
  • imgSWIR1
  • imgVNIR2
  • imgSWIR2
  • imgVNIR3
  • imgSWIR3

Réponse acceptée

Adam
Adam le 16 Déc 2014
Modifié(e) : Adam le 16 Déc 2014
Use a cell array instead:
imgVNIR{1} = ...
imgVNIR2{2} = ...
imgSWIR{1} = ...
imgSWIR{2} = ...
Having millions of variables with incremental numbers is almost never a good solution to a problem and requires you to do gymnastic with the Matlab language to achieve it. Then when you have created them you have to do something with the mess of variables you have created instead of just a single (or a few) nice cell arrays that you can numerically index into in a single easy statement.
I just glanced at your specific problem, but if all your results are of the same dimension then it is also possible you can just use a numeric array of dimension n + 1 (for results of dimension n) and index into that using the n th dimension to retrieve the result you want. This will be faster to execute than a cell array, but is slightly less intuitive to use (in my opinion).

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by