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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by