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
5
  • 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? Commented Nov 13, 2014 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. Commented Nov 13, 2014 at 18:57
  • I updated my answer after your command @Mark Commented Nov 13, 2014 at 18:57
  • 2
    What's wrong with a 2D array? Intensity(m,n) = I; Commented Nov 13, 2014 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. Commented Nov 13, 2014 at 20:35

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

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 Agreed, but if it is quick simulation/calculation that he needs to be done, he can get away with it.
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.”

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.