MATLAB Answers


Delimate Table and Rewrite it

Asked by Seum Bin Rahman on 13 Oct 2019
Latest activity Commented on by Peter Perkins
on 15 Oct 2019
I have a Table. One of the colums has multiple strings seperated by comma. The table is as follows;
_______________________ ____________________________________
'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
for i=1:1:P
C = strsplit(PROBLEMS,',');
Error is as follows
Error using t3 (line 15)
Subscripted assignment dimension mismatch for table variable 'C'.
Excel file is also attached


Sign in to comment.

1 Answer

Answer by Walter Roberson
on 13 Oct 2019
 Accepted Answer

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'})


My God it worked!! I dont understand the first two lines. But its okey. You may explain if you are free
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.
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'}
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).
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.

Sign in to comment.