Conversion problem of data types of an array

1 visualización (últimos 30 días)
cbh0307
cbh0307 el 14 de Sept. de 2017
Comentada: cbh0307 el 15 de Sept. de 2017
Hi all, I read with textscan from a text file to get an array. I received this error report at first
"All contents of the input cell array must be of the same data type."
for my array "data_A".
data_A:
522x1 double 522x1 double 522x1 cell 522x1 double
I tried to do this:
data_A(:,3) = str2double(data_A(:,3));
and I received this report:
"Conversion to cell from double is not possible."
Hence, I tried to use cell2mat:
data_A(:,3) = cell2mat(data_A(:,3));
for which I got the same error again:
"All contents of the input cell array must be of the same data type."
It would be so kind, if anyone could look into the matter. Any help and tips are very much appreciated. =)
Best regards,
Boon
  4 comentarios
dpb
dpb el 14 de Sept. de 2017
So, what is in the data record that is screwing up the reading of what looks as should be number? The ',' is a normal delimiter so that shouldn't be the problem.
Fix the problem at its root rather than try to post-process a mess created where needn't be (at least until can prove it must be a mess).
Show an offending record and how you tried to read it.
cbh0307
cbh0307 el 14 de Sept. de 2017
Thank you dpb!

Iniciar sesión para comentar.

Respuesta aceptada

Guillaume
Guillaume el 14 de Sept. de 2017
I'm with dpb, fix the code so that it does not make a mess in the first place rather than trying to fix the mess.
It sounds like you're importing some data with something like textscan which needs you to explicitly specify the format of the text file. Since R2013b, matlab has included readtable which is usually capable of figuring out the format (and headers) on its own. Perhaps, moving to more modern constructs would solve the problem.
Most time all that is needed is:
t = readtable(yourfile);
Nothing more, and if readtable gets it wrong, you can guide it with detectImportOptions and specify the actual format for these columns where it gets it wrong.
  3 comentarios
Guillaume
Guillaume el 14 de Sept. de 2017
Consider preallocating for speed. Most likely, you haven't preallocated data_A_new, so at each step of your loop, matlab resizes it make room for the row you add.
In addition, you need to learn how to access the content of a cell array. data_A{1, i} (note the curly braces) is a lot more efficient than cell2mat(data_A(1, i)).
So, with that said:
data_A_new = zeros(numel(data_A{1, 1}, size(data_A, 2)); %preallocation
for i = 1 : size(data_A, 2) %don't hardcode sizes
data_A_new(:, i) = data_A{1, i};
end
But, as usual with matlab, the above can be achieved more simply without a loop:
%if the content of data_A{1, i} is all column vectors:
data_A_new = cell2mat(data(1, :));
%if the content of data_A{1, i} is all row vectors:
data_A_new = cell2mat(data(1, :)).';
%otherwise:
data_A_new = cellfun(@(x) x(:), data_A(1, :), 'UniformOutput', false); %to reshape every matrix into column vectors
data_A_new = cell2mat(data(1, :));
cbh0307
cbh0307 el 15 de Sept. de 2017
Thanks again Guillaume!! I did learned a lot from your comment. The warning is no more as well. My gratitude.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre App Building 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!

Translated by