save all for loop data into workspace

9 visualizaciones (últimos 30 días)
tarek abousaleh
tarek abousaleh el 12 de Mzo. de 2018
Respondida: Bob Thompson el 12 de Mzo. de 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

Respuesta aceptada

Bob Thompson
Bob Thompson el 12 de Mzo. de 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

Más respuestas (1)

Geoff Hayes
Geoff Hayes el 12 de Mzo. de 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.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by