Cell Array of Strings Indexing and Relabeling to Numeric Values?

2 ビュー (過去 30 日間)
Erin Winkler
Erin Winkler 2018 年 7 月 18 日
回答済み: dpb 2018 年 7 月 18 日
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!

採用された回答

dpb
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 件)

カテゴリ

Help Center および File ExchangeCategorical Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by