save all for loop data into workspace

7 views (last 30 days)
I have a for loop, but every iteration overwrites the variable and I have only the final data left.. how can I save data from every loop the new images i am having have different matrices when i dicomread them because they are from type uint and not double saves only the last value when R=172. how do I get the other values?
for c=1:172
name1 = strcat(folder_name1,'/',filesStruct1(c).name);
alpha = dicomread(name1)
R = (((double(alpha).*pi)./(4096)) - (pi./2))
dicomwrite(R, (strcat('Results/',filesStruct1(c).name)));
end
please if somebody can help me by editing that on my code, I am kind of using matlab since a few months thanks in advance

Accepted Answer

Bob Thompson
Bob Thompson on 12 Mar 2018
Easiest way to do this is with indices within your code. Instead of having R be the value you are writing, you want to write R(c), or R{c} depending on the type of data R contains. The first is for a regular double array, while the second is for a cell.
for c=1:172
name1 = strcat(folder_name1,'/',filesStruct1(c).name);
alpha = dicomread(name1)
R(c) = (((double(alpha).*pi)./(4096)) - (pi./2))
dicomwrite(R(c), (strcat('Results/',filesStruct1(c).name)));
end

More Answers (1)

Geoff Hayes
Geoff Hayes on 12 Mar 2018
tarek - use a cell array to store the data (R) on each iteration of the for loop. For example,
myImageData = cell(172,1);
for c=1:172
name1 = strcat(folder_name1,'/',filesStruct1(c).name);
alpha = dicomread(name1)
R = (((double(alpha).*pi)./(4096)) - (pi./2))
dicomwrite(R, (strcat('Results/',filesStruct1(c).name)));
myImageData(c) = R;
end
Once you have iterated over all 172 images, then the myImageData cell array will have the R from each iteration.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!