save all for loop data into workspace

조회 수: 7 (최근 30일)
tarek abousaleh
tarek abousaleh 2018년 3월 12일
답변: Bob Thompson 2018년 3월 12일
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

채택된 답변

Bob Thompson
Bob Thompson 2018년 3월 12일
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

추가 답변 (1개)

Geoff Hayes
Geoff Hayes 2018년 3월 12일
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.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by