Reading cell array from excel table, which contains different sizes of doubles

2 visualizaciones (últimos 30 días)
I would like to read these columns as cell arrays, where the components are doubles, such as the following:
m_con = {[1,2,3],2,3};
I have tried xlsread but it didn't work.

Respuesta aceptada

Adam Danz
Adam Danz el 12 de En. de 2021
Editada: Adam Danz el 12 de En. de 2021
There might be a smoother method but this works with the data from your image.
file = 'Book1.xlsx';
opts = detectImportOptions(file);
opts = setvartype(opts, 'char');
C = readcell(file,opts); % you can also try readtable()
numIdx = cellfun(@isnumeric,C);
C(numIdx) = cellfun(@num2str,C(numIdx),'UniformOutput',false);
Cnum = cellfun(@str2num,C,'UniformOutput',false)
Cnum =
3×4 cell array
{1×3 double} {1×3 double} {[ 1]} {[ 1]}
{[ 2]} {[ 2]} {1×2 double} {1×2 double}
{[ 3]} {[ 2]} {1×2 double} {1×2 double}
Cnum{1,1}
ans =
1 2 3
  8 comentarios
Ege Arsan
Ege Arsan el 12 de En. de 2021
I have one last quastion. I tried to combine these two with an if condition but i still get en error. Do you know a better expression or is it completely false waht i did?
if ~isnumeric(C)
numIdx = cellfun(@isnumeric,C);
C(numIdx) = cellfun(@num2str,C(numIdx),'UniformOutput',false);
Cnum = cellfun(@str2num,C,'UniformOutput',false);
elseif ~ischar(C)
charIdx = cellfun(@ischar,C);
Cnum(charIdx) = cellfun(@str2num,C(charIdx),'UniformOutput',false);
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Import from MATLAB 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