how to save every iteration into workspace

6 views (last 30 days)
my code would save just the last iteration.
for c=1:170
name1 = strcat(folder_name1,'/',filesStruct1(c).name);
alpha = (((double(dicomread(name1)).*pi)./(4096)) - (pi./2))
dicomwrite(alpha, (strcat('Results/',filesStruct1(c).name)));
save(mfilename)
end
can you please edit my code so it saves alpha 170 times with 170 different 256*240 matrices thanks in advance

Accepted Answer

Stephen23
Stephen23 on 12 Mar 2018
Edited: Stephen23 on 12 Mar 2018
Use fullfile rather than string concatenation. To store the imported images in a loop simply preallocate a cell array before the loop, and then use indexing:
N = 170;
C = cell(1,N);
for k = 1:N
name = fullfile(folder_name1,filesStruct1(c).name);
alpha = double(dicomread(name1))*pi/4096 - pi/2
dicomwrite(alpha, fullfile('Results',filesStruct1(c).name));
C{k} = alpha;
end
save(mfilename,'C')
  3 Comments
Stephen23
Stephen23 on 12 Mar 2018
Edited: Stephen23 on 12 Mar 2018
@dpb: you forgot to consider the cell array itself. If the cell array itself has to be enlarged, then it must be moved in memory, and thus causes a performance penalty. Note that this has nothing to do with the contents of the cells (as you assumed), but is solely because of the cell array itself (which is essentially an array of pointers to other arrays) changing size and requiring to be moved in memory.
This topic is explained the MATLAB documentation:
and similarly for structures, which was discussed in some detail in Loren Shure's blog:
and on this forum, e.g.:
Note also that the MATLAB preallocation documentation clearly recommends using cell to preallocate cell arrays.
The critical information to understanding this is to appreciate that any cell array or structure array is already an array in its own right (of pointers), and that means that expanding them repeatedly will be slow. Always preallocate cell arrays and structure arrays.
dpb
dpb on 12 Mar 2018
Hadn't researched the cell/structure array sufficiently. I had presumed they would be implemented more like a linked list than actual arrays and therefore "not so much" a performance hit. So OP needs to put an allocation step after determining the size in my above answer... :)

Sign in to comment.

More Answers (2)

KL
KL on 12 Mar 2018
Edited: KL on 12 Mar 2018
You're overwriting alpha during every iteration. Replace alpha with alpha(c) and then move the save command outside the loop.
  4 Comments
KL
KL on 12 Mar 2018
my bad. It should have been cell arrays. alpha{c} like dpb has shown.
tarek abousaleh
tarek abousaleh on 12 Mar 2018
it is still not working and i am getting an errorrror using dicom_prep_ImagePixel>getPixelStorage (line 204) Invalid datatype for image pixels.
Error in dicom_prep_ImagePixel (line 13) [ba, bs, hb, pr] = getPixelStorage(X, txfr, useExistingBitDepths, metadata, dictionary);
Error in dicom_prep_metadata (line 51) metadata = dicom_prep_ImagePixel(metadata, X, map, txfr, useMetadataBitDepths, dictionary);
Error in dicom_create_IOD (line 26) metadata = dicom_prep_metadata(IOD_UID, metadata, X, map, options.txfr, options.usemetadatabitdepths, dictionary);
Error in dicomwrite>write_message (line 271) [attrs, status] = dicom_create_IOD(SOP_UID, X, map, ...
Error in dicomwrite (line 208) [status, options] = write_message(X, filename, map, metadata, options);
Error in phase (line 43) dicomwrite(alpha, (strcat('Results/',filesStruct1(c).name)));

Sign in to comment.


dpb
dpb on 12 Mar 2018
Edited: dpb on 12 Mar 2018
n=length(filesStruct1); % presuming filesStruct1 is returned from DIR()
alpha=cell(n,1); % listen to the pundits and preallocate even cell array :)
for c=1:n
name1 = fullfile(folder_name1,filesStruct1(c).name);
alpha{c} = double(dicomread(name1)*pi)/4096 - pi/2;
end
...
NB: the "curlies" {} to create cell array alpha.
Alternatively you could read the first to determine
[r,c]=size(alphaOne); % first in case possibly use differing size arrays
alpha=zeros(r,c,n); % preallocate a 3D array, put each into slice (plane)
alpha(:,:,1)=alphaOne); % save first
clear alphaOne % minor cleanup; don't need any longer
for c=2:n % now do the rest
alpha(:,:,c)= ........
...
This way one has a default double array instead of cell array so can save a dereferencing step in use or, depending on what is to be done, making calculations across the various planes is much simpler if that were to be wanted/required...

Categories

Find more on Structures 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!