Delimate Table and Rewrite it
조회 수: 2 (최근 30일)
이전 댓글 표시
I have a Table. One of the colums has multiple strings seperated by comma. The table is as follows;
MODELS PROBLEMS
_______________________ ____________________________________
'Olvio L25' 'CHARGING,keypad lock,DEAD'
'Olvio L26' 'DEAD,Auto ON OFF'
'Olvio L26' 'KEYPAD'
I wish to delimate column "PROBLEMS" and save it back to another table which will have three columns, POBLEMS1, PROBLEMS2, PROBLEMS3.
The code is as follows
raw=readtable('Service.xlsx');
r=0;
raw_problem=raw.Expert_Found_Problems;
P=height(raw);
for i=1:1:P
PROBLEMS=raw_problem{i,1};
C = strsplit(PROBLEMS,',');
r=r+1
T(r,:)=table(C)
end
T=T
Error is as follows
Error using t3 (line 15)
Subscripted assignment dimension mismatch for table variable 'C'.
Excel file is also attached
댓글 수: 0
채택된 답변
Walter Roberson
2019년 10월 13일
table() applied to the results of strsplit is probably giving you a table with one variable and three rows.
You need to take special steps when the input is a vector because there is an ambiguity over whether to create a table with one row and several variables, or a table with several rows and one variable.
table(C{1},C{2},C{3}, 'VariableNames', {'Problem1', 'Problem2', 'Problem3'})
댓글 수: 5
Walter Roberson
2019년 10월 13일
In MATLAB, there is no direct (and readable) way to call a function and immediately select a subset of the result. It would be nice if it were permitted to do something like,
[V{:}, {'','',''}](1:3) %not actually permitted
but MATLAB does not provide for that. It does, though, make it possible to call a function on a computed result, and for the function to take the subset.
Thus,
Pad3 = @(V) First3([V{:}, {'','',''}]);
will be passed a 1 x something cell array with a varying number of elements in it, from 0 to 3. The {:} part expands the cell into individual arguments of the [] function it is inside; that will expand to 0, 1, 2, or 3 arguments. Then the cell with 3 empty strings is appended through the [] operator, which gives you a cell result with 3, 4, 5, or 6 elements, with the last 3 being empty in each case. Then First3 is called on that, returning the first 3 of the elements. So {} the empty cell would become {'' '' ''} and {'one'} would become {'one' '' ''} and {'one' 'two'} would become {'one' 'two' ''} and {'one' 'two' 'three'} would stay {'one' 'two' 'three'}.
The raw_problem = raw.Expert_Found_Problems line is going to give you a cell array column vector each containing a character vector. The C = regexp(raw_problem, ',', 'split') line is going to give you a cell array the same size, with each entry being a cell array that has as many entries as there are comma deliminated parts. Like,
{
{'CHARGING' 'keypad lock' 'DEAD'}
{'DEAD' 'AUTO on off'}
{'KEYPAD'}
}
The arrayfun() is going to process each of those rows, so it is going to pass PAD3 {'CHARGING' 'keypad lock' 'DEAD'} first, then {'DEAD' 'AUTO on off'} then {'KEYPAD'} . As indicated above, that will convert to
{
{'CHARGING' 'keypad lock' 'DEAD'}
{'DEAD' 'AUTO on off' ''}
{'KEYPAD' '' ''}
}
Notice that all of the entries now contain exactly the same number of columns.
Now that they all contain the same number of columns, the line
C = vertcat(C{:});
converts that to
{
'CHARGING' 'keypad lock' 'DEAD'
'DEAD' 'AUTO on off' ''
'KEYPAD' '' ''
}
which is a something-by-3 cell array.
With that in place, cell2table() constructs a table with 3 variables (one for each column).
Peter Perkins
2019년 10월 15일
In recent versions of MATLAB, you could also overwrite raw.PROBLEMS (or whatever it's called, I can't tell) with Walter's C, a "something-by-3 cell array", and then use splitvars on raw to split it into three separate variables.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!