Delimate Table and Rewrite it
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Seum Bin Rahman
am 13 Okt. 2019
Kommentiert: Peter Perkins
am 15 Okt. 2019
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 Kommentare
Akzeptierte Antwort
Walter Roberson
am 13 Okt. 2019
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 Kommentare
Walter Roberson
am 13 Okt. 2019
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
am 15 Okt. 2019
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Tables finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!