How to concatenate a set of rows created in a for loop?
3 views (last 30 days)
Show older comments
I'm trying to create a 2D array by concatenating a set of 1D column arrays created in a for loop. The code I have so far.
fileList = {};
warning('off', 'all');
% Ask user for directory input and gather subfolders
[filePath] = uigetdir('','Select Directory of Folders to be Loaded');
fileList = dir(filePath);
fileList = fileList(arrayfun(@(x) x.name(1), fileList) ~= '.');
% Create array of files
dat = cell(size(fileList, 1), 6);
for j = 1:size(fileList, 1)
% Open array of file and gather info
fID = fileList(j).name;
fID = strcat(filePath, '\',fID);
info = dicominfo(fID);
pID = info.PatientID;
serNo = info.SeriesNumber;
sliceLoc = info.SliceLocation;
repet = info.RepetitionTime;
echo = info.EchoTime;
% Construct array of files with info
dat(j,:) = {fID, pID, serNo, sliceLoc, repet, echo};
end
% Sort array by echo time
dat = sortrows(dat, 6);
% Read files according to echo time
for j = 1:length(dat)
[img] = dicomread(char(dat(j,1)));
images(:,:,j) = img;
end
% plot T2 by matching each pixel, use T(1, 1, :) and iterate for all
% pixels.
vals = cell(size(images, 3), 2);
T2ind = cell(size(images, 1), 1);
T2 = cell(size(images, 1), size(images, 2));
% Loop through all rows
for y = 1:size(images,1)
% Loop through all colunms
for x = 1:size(images,2)
% If the average value is greater than 100 for the given row and colunm number then loop through all 'pages'
if ((sum(images(y,x,:))/size(images, 3)) > 100)
for z = 1:size(images, 3)
% Read echo time and pixel values and constuct array
echo = dat(z, 6);
imgVal = images(y,x,z);
vals(z,:) = {echo, imgVal};
end
% Fit an exponential curve to pixel values and calculate T2
f = fit(cellfun(@cell2mat, vals(:,1)), cell2mat(vals(:,2)), 'exp1');
coeff = coeffvalues(f);
a= coeff(1);
b = coeff(2);
S1 = f(10);
S2 = S1/exp(1);
T2ind{x} = ((log(S2/a))/b);
end
end
T2(:,y) = (T2ind(:));
end
% T2(T2 < 0) = 0;
% T2(isnan(T2)) = 0;
The error I get is...
Subscripted assignment dimension mismatch.
Error in T2_mapping (line 61)
T2(:,y) = cat(2, T2ind{:});
Any help? Thanks!
6 Comments
Answers (1)
Guillaume
on 24 Feb 2015
Edited: Guillaume
on 24 Feb 2015
If I read your code right (comments needed!) T2Ind is a cell array of scalar or empties. Therefore horzcat(T2Ind{:}) is a vector of numbers. You're trying to assign that to a m*1 cell array (with m = size(images, 2)), so of course it's not going to work. I'm not sure what you're trying to do, so can't really suggest how to correct it.
Further to my comment above, it's possible you don't need all your loops. Again, I've not wrapped my head around the whole of your code, but
sum(images(y,x,:))/size(images, 3)
could be just calculated outside the loop as:
imagesmean = mean(images, 3);
Furthermore, what the heck is that supposed to do:
cell2mat(cellfun(@cell2mat, vals(:,1), 'UniformOutput', false))
5 Comments
Guillaume
on 24 Feb 2015
@Shaun,
Yes, I understood the purpose of your loop and I'm fairly certain they're not necessary or can be reduced to just one loop. The obscure bit is all this weird cell manipulation with echo and as mentioned what you're trying to do with your cell array assignment on the line with the error.
See Also
Categories
Find more on Creating and Concatenating Matrices 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!