Domanda

Quick question,

I would like to make a count from 50-70 using sprintf in Matlab. This example prints 0101-0120

for i = 1:20
    filename = sprintf('Brain_01%02d.dcm', i);
    [X(:,:,1,i), amap] = dicomread(filename);
end

How would I change this to print 0151-0170?

È stato utile?

Soluzione

The answer seemed obvious at first, but it seems like another issue might be related to the indexing of X getting broken if i doesn't start at one. Here's one way to address that while handling pre-allocation of X,

imgInds = 151:170;
di = dicominfo(sprintf('Brain_%04d.dcm',imgInds(1)));
X = zeros(di.Height,di.Width,1,numel(imgInds),class(dicomread(di))); % modify

for i = 1:numel(imgInds),
    filename = sprintf('Brain_%04d.dcm', imgInds(i));
    [X(:,:,1,i), amap] = dicomread(filename);
end

For clarity, I think it is better to build your sprintf with %04d instead of 01%02d. You should set the size of X accordingly on the line labeled modify, particularly the third dimension since I assume your actual code will not have this be 1.

Altri suggerimenti

I'm guessing this should do it:

for i = 51:70
    filename = sprintf('Brain_01%02d.dcm', i);
    [X(:,:,1,i), amap] = dicomread(filename);
end

Thank you for your responses! Actually all I needed to do (for my purposes) was:

for i = 1:20
filename = sprintf('Brain_01%02d.dcm', i + 49);
[X(:,:,1,i), amap] = dicomread(filename);
end

which made the count start from 50.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top