0

This script is being used for image processing by multiplying a set of 2000 images with a mask and then summing the values in each frame. These values are entered into a row vector called Intensity.

I am trying to end up with 20 row vectors called intensity1, intesity2...intensity20, is there a straight forward way to change the name of the Intensity row vector upon every loop iteration?

for m=1:20   

 mask=bigrating(m,m,0);

        for n=1:2000  
            I=sum(sum(imread((sprintf('image%05d.tif',n))).*(mask)));
            Intensity(n)=I;
        end

save('filepath','Intensity')

end
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
Greg
  • 1
  • 1
  • 4
  • 1
    I am slightly confused, you want to write each of the intensity vectors into a different file naming like intensity1, intensity2,....intensity20? Could you clarify what you mean by "Change the name of the intensity row vector". Or do you want to dynamically create intensity1, intensity2,..etc. instead of doing Intensity(n) in the for loop? – ha9u63a7 Nov 13 '14 at 18:49
  • I would like to save the intensity vectors as intensity1,intensity2 etc. The name of the file increments by 1 each time and i would like the name of the variables saved within the files to increment by each time also. – Greg Nov 13 '14 at 18:57
  • I updated my answer after your command @Mark – ha9u63a7 Nov 13 '14 at 18:57
  • 2
    What's wrong with a 2D array? `Intensity(m,n) = I;` – Peter Nov 13 '14 at 19:20
  • 1
    Do you *need* to have different variable names? It's clumsy and slow. Pre-allocate a 2D array and update it like @Peter suggests. – Cape Code Nov 13 '14 at 20:35

1 Answers1

0

Because you wanted dynamically named intensity1, intensity2,....intensity20 etc, the following should work for you:

for m = 1:20
    mask = bigrating(m,m,0)
    for n = 1:2000
        I=sum(sum(imread((sprintf('image%05d.tif',n))).*(mask)));
        eval(['intensity' num2str(m) ' = I'])
    end
    save('filepath', ['intensity' num2str(m)])
end
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
  • The use of `eval` is generally not recommended and should avoided unless there is no other solution. Here, a 2D array as @Peter suggested is a much better solution t the problem. – am304 Nov 13 '14 at 21:12
  • @am304 Agreed, but if it is quick simulation/calculation that he needs to be done, he can get away with it. – ha9u63a7 Nov 13 '14 at 21:16
  • I'm just slightly wary to teaching people, particularly new users, bad things because when they've done it once, they'll keep doing it, rather than do it properly. As Master Yoda once said: "If once you start down the dark path, forever will it dominate your destiny, consume you it will, as it did Obi-Wan’s apprentice.” – am304 Nov 13 '14 at 21:25