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.

 채택된 답변

Guillaume
Guillaume 2018년 11월 12일

1 개 추천

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

James Bader
James Bader 2018년 11월 13일
Wow, this is literally perfect. Thank you so much this will make my loops much easier!!!
madhan ravi
madhan ravi 2018년 11월 13일
+1 rowfun newly learnt!
Guillaume
Guillaume 2018년 11월 13일
편집: Guillaume 2018년 11월 13일
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).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

질문:

2018년 11월 12일

편집:

2018년 11월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by