save all for loop data into workspace

7 Ansichten (letzte 30 Tage)
tarek abousaleh
tarek abousaleh am 12 Mär. 2018
Beantwortet: Bob Thompson am 12 Mär. 2018
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

Akzeptierte Antwort

Bob Thompson
Bob Thompson am 12 Mär. 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

Weitere Antworten (1)

Geoff Hayes
Geoff Hayes am 12 Mär. 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.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by