Cell Array of Strings Indexing and Relabeling to Numeric Values?
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi all,
I have a cell array of strings that mark certain sites (example: 'Townsville - Crossroad Station (1234)'). I have 27 different sites in total.
What I need help with is, this is a column in a table with other data and I wanted to change the strings to numbers.
Something like this:
if site(i) == 'Townsville - Crossroads Station (1234)'
site(i) = 0;
if site(i) == 'City Colonies - Fork in Road (5678)'
site(i) = 1;
.
.
.
else
site(i) = 26;
I want this so I can then make subset matrices of values from the table filtered by the condition without typing a huge string each time. I could also then treat it as categorical data as a factor more easily.
Or, if it's possible to do this, if there is a way to loop through and set a string = number (like above) but index the number it sets each time the string is different, that would be great too.
The main components of the table I'm evaluating are: datetimes, site, day of week
right now and I want to section off by site first.
Any help would be great, thank you!
댓글 수: 0
채택된 답변
dpb
2018년 7월 18일
This is perfect use for categorical variable...and to use a |table instead of cell array for the data structure. Life will be much easier then... :)
Attach a .mat file with your input file and someone here can (and probably will) get you quite a ways along very quickly...
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Categorical Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!