MATLAB Answers

0

how to save every iteration into workspace

Asked by tarek abousaleh on 12 Mar 2018
Latest activity Edited by dpb
on 12 Mar 2018
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

  0 Comments

Sign in to comment.

3 Answers

Answer by Stephen Cobeldick on 12 Mar 2018
Edited by Stephen Cobeldick on 12 Mar 2018
 Accepted Answer

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

dpb
on 12 Mar 2018
Does preallocating a cell array really make any difference, Stephen? I've never really seen how it would/could since it's unknown what the cell content will be and it can be (almost) anything in each and every cell.
@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
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.


Answer by KL
on 12 Mar 2018
Edited by 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

Show 1 older comment
I have tried doing that from a tutorial but every time i replace alpha with alpha(c) i got an error
KL
on 12 Mar 2018
my bad. It should have been cell arrays. alpha{c} like dpb has shown.
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.


Answer by dpb
on 12 Mar 2018
Edited by 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...

  1 Comment

it worked thanks a lot

Sign in to comment.