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

조회 수: 5 (최근 30일)
Thisis Idea
Thisis Idea 2023년 11월 12일
답변: Jeremy Hughes 2024년 1월 4일
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
Stephen23
Stephen23 2023년 11월 12일
편집: Stephen23 2023년 11월 12일
"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.
Kyle
Kyle 2023년 11월 18일
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.

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

답변 (1개)

Jeremy Hughes
Jeremy Hughes 2024년 1월 4일
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'}

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by