Separate one column in an imported matrix

Hello,
Basically, I have a table where the last column really has several values. The number of these values differ. What I would like is to import the data so that it is all in a matrix and instead of one column showing points, you have a subsequent series of columns from (point one) to (point n). It is fine to fill zeros so that every row has the same number of vectors. I attached a portion of my data sheet. MatLab tries to sum or multiply these values when importing; I took a first pass at trying to use textscan and splitcells and other things I saw on the forum but I cannot get it to run correctly. Anyone know the easiest way to do this? I said C=textscan(segments1,'%f') and got that the first input must be a valid file-id or non-empty character vector.

 Respuesta aceptada

Guillaume
Guillaume el 12 de Nov. de 2018
The way I'd do it:
t = readtable('test11_1.txt'); %import as table
t.PointIDs = rowfun(@(s) sscanf(s, '%f,', [1 Inf]), t, 'InputVariables', 'PointIDs', 'ExtractCellContent', true, 'OutputFormat', 'cell')
It's probably better to keep the data in a table rather than storing it in a matrix, particularly one padded with 0s.

3 comentarios

James Bader
James Bader el 13 de Nov. de 2018
Wow, this is literally perfect. Thank you so much this will make my loops much easier!!!
madhan ravi
madhan ravi el 13 de Nov. de 2018
+1 rowfun newly learnt!
Guillaume
Guillaume el 13 de Nov. de 2018
Editada: Guillaume el 13 de Nov. de 2018
Note that you could use cellfun just the same:
t.PointIDs = cellfun(@(s) sscanf(s, '%f,', [1 Inf]), t.PointIDs, 'UniformOutput', false);
In this particular case, rowfun doesn't have a particular benefit over cellfun. In other cases, it can be extremely useful (particularly, with its GroupingVariables option).

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 12 de Nov. de 2018

Editada:

el 13 de Nov. de 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by