Dynamically change variable name inside a loop in MATLAB

落爺英雄遲暮 提交于 2020-01-06 14:45:13

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/26916160/dynamically-change-variable-name-inside-a-loop-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!