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!