How can I split comma-separated characters inside a cell of a cell array?

I have Cell array element extracted from xls file as
C={'NORMAL,HI,LOW,MEDIUM,OUTOFR'}
first I want to count the words with delimeter as",", then assign values to these as
NORM=0
LOW=1
HI=2
MEDIUM=3
OUTOFR=4
I tried strsplit by converting the cell to string but it gave same string as
"NORM,HI,LOW,MEDIUM,OUTOFR"

6 Comments

Firstly, strsplit is for a string scalar, or a char vector.
For a cell array of char vectors, use split.
C = {'NORMAL,HI,LOW,MEDIUM,OUTOFR'}
C = 1×1 cell array
{'NORMAL,HI,LOW,MEDIUM,OUTOFR'}
D = split(C, ',')
D = 5×1 cell array
{'NORMAL'} {'HI' } {'LOW' } {'MEDIUM'} {'OUTOFR'}
Secondly, Why do you want to assign variables dynamically?
What you want to do is not recommended, as it is extremely difficult to work with.
C = {'NORMAL,HI,LOW,MEDIUM,OUTOFR'}
C = 1×1 cell array
{'NORMAL,HI,LOW,MEDIUM,OUTOFR'}
D = strsplit(string(C), ',')
D = 1×5 string array
"NORMAL" "HI" "LOW" "MEDIUM" "OUTOFR"
or
D = strsplit(string(C{:}), ',')
D = 1×5 string array
"NORMAL" "HI" "LOW" "MEDIUM" "OUTOFR"
But, as @Dyuman Joshi notes, the newer split function is somewhat more forgiving/handy. As with so many things, there are too many options available as new stuff gets added on rather than upgrading existing; one has a hard time to know which to choose and why.
As for the second part, to amplify a little more, don't store metadata as variable names, create a variable called "levels" or other meaningful name and then this looks like could be good place for a categorical variable...
D=D([3:4 1:2 5]).';
levels=categorical(D,D,D,'ordinal',1)
levels = 5×1 categorical array
LOW MEDIUM NORMAL HI OUTOFR
If you do need a numerical value for some reason, then
double(levels)
ans = 5×1
1 2 3 4 5
Thanks I am staisfied with the answer.
I was trying with only D=strsplit(C) ...now i got the reason why strsplit was not working.
Thanks for the response.
Yes "split" is better option. and it worked with my 8x1 Cell array as there were more elements like this.
About assigning variables ..no i am not doing it dynamically. I call these splitted words as values and I will assign the numbers to the words in alphabetical order.
Thanks .
"About assigning variables ..no i am not doing it dynamically. I call these splitted words as values and I will assign the numbers to the words in alphabetical order."
That sounds exactly like dynamically creating variable names using meta-data (and is also what you show in your question).
A table may be a good choice for that data.
How are you getting this cell array from the .xls file? It sounds like you might be able to simplify your code a lot if you let MATLAB import the data for you. You could use the Import Tool or Live Editor Import Data task and either will give you reusable code to import from that file programatically. Alternatively, if you already know your data, you could also write your own code using readtable to import data with row names.

Sign in to comment.

Answers (1)

C={'NORMAL,HI,LOW,MEDIUM,OUTOFR'}
C = 1×1 cell array
{'NORMAL,HI,LOW,MEDIUM,OUTOFR'}
split(C,",")
ans = 5×1 cell array
{'NORMAL'} {'HI' } {'LOW' } {'MEDIUM'} {'OUTOFR'}

Categories

Asked:

on 12 Nov 2023

Answered:

on 4 Jan 2024

Community Treasure Hunt

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

Start Hunting!