why dosen't this work ? i need a (:,4) cell. but 90 % of my result is empty spaces.
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Anne
el 27 de Mzo. de 2017
Comentada: Anne
el 27 de Mzo. de 2017
PLS = [PIDSorted; NameSorted; ExamSorted; DateSorted];
NewPL = cell(size(PLS,1),4); %Patients sorted
for n=1:size(PLS,1);
if size(PLS{n,:},1)==1 ;
NewPL{1,n} = PIDSorted(n,:);
NewPL{2,n}= NameSorted{n};
NewPL{3,n}=ExamSorted{n};
NewPL{4,n}=DateSorted{n};
end
end
I tried to use NewPL = NewPL(~cellfun('isempty',NewPL)) but then I have my result in only one column
0 comentarios
Respuesta aceptada
Guillaume
el 27 de Mzo. de 2017
Editada: Guillaume
el 27 de Mzo. de 2017
There's not much in your code that makes sense.
First issue:
if size(PLS{n, :}, 1) == 1
PLS{n, :} implies that PLS has more than one column (since the column says "return all columns"), yet if the PLS cell array has more than one column, size will error (invalid number of arguments). So, we're assuming that PLS is just one column, in which case
if size(PLS{n}, 1) == 1
would be a lot better.
Second issue:
NewPL = cell(size(PLS,1),4)
for n=1:size(PLS,1)
if ...
NewPL{1,n} = ...
NewPL{2,n} = ...
NewPL{3,n} = ...
NewPL{4,n} = ...
You create a h x 4 cell array, yet your code in the if block fills a 4 x h cell array (with h being size(PLS, 1)). This would cause an 'index exceeds matrix dimension' error as soon as n is greater than 4. If you don't get an error that means the if block is never entered, which must mean that the matrices in PLS have more than one row.
Third issue:
PLS = [PIDSorted; NameSorted; ExamSorted; DateSorted];
for n=1:size(PLS,1)
if ...
... = PIDSorted(n,:);
... = NameSorted{n};
... = ExamSorted{n};
... = DateSorted{n};
n goes from 1 to the number of rows in PLS. The number of rows in PLS is the sum of the number of rows in your 4 variables. So, unless the variables are empty, n is guaranteed at some point to be more than the number of rows in any of these variable. So clearly, if the if block gets entered, var{n} is going to error with 'index exceeds matrix dimension' again.
I'm guessing that you're trying to concatenate a PIDSorted matrix with n rows with cell arrays with also n rows. In that case, all that is needed to create your cell array is:
NewPL = [num2cell(PIDSorted, 2), NameSorted, ExamSorted, DateSorted]
10 comentarios
Guillaume
el 27 de Mzo. de 2017
For some reason, it was missing the mod call. Now fixed:
NewPL = reshape([num2cell(PIDSorted, 2), NameSorted, ExamSorted, DateSorted; cell(mod(-numel(NameSorted), 4), 4)]', [], 4);
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Computations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!