How read string or enumeration from excel and convert to numeric values in matlab script

6 ビュー (過去 30 日間)
VINAY
VINAY 2025 年 6 月 19 日
回答済み: Deepak 2025 年 6 月 23 日
ex:
READ
GREEN
YELLOW
read from excel sheet and convert into numeric values (READ =1, GREEN =2, YELLOW=3)
  2 件のコメント
Steven Lord
Steven Lord 2025 年 6 月 19 日
What are you hoping to do with these numeric values afterwards?
Do you specifically want READ (I assume a typo and it's actually supposed to be RED) to be 1 and GREEN to be 2 etc., or is that just because that's the order they happened to appear in the file?
You may want to read the data in as a categorical array. If you don't actually need them to be numbers but just need them to be usable as grouping variables in calculations like groupsummary, grouptransform, etc. then that would be easiest.
VINAY
VINAY 2025 年 6 月 20 日
What are you hoping to do with these numeric values afterwards?
need to feed into the simulink blocks (for better understading)

サインインしてコメントする。

回答 (1 件)

Deepak
Deepak 2025 年 6 月 23 日
Hi @VINAY,
I understand that you want to read string values like 'RED', 'GREEN', 'YELLOW' from an Excel sheet and convert them into numeric codes (e.g., RED = 1, GREEN = 2, etc.) to feed into Simulink blocks. You can do this by reading the data using "readtable", then mapping the strings to numbers using either a "containers.Map", "dictionary" (for R2022b and later), or a "categorical" array. For example, dictionary(["RED","GREEN","YELLOW"], [1,2,3]) allows you to convert a string array to numeric values with a simple lookup.
Once you have the numeric values, you can pass them into Simulink using a Constant block for static input, or a From Workspace block if you want to feed a time-based signal (e.g., for simulation). This is a common approach for mapping enums or string labels into Simulink-compatible numeric formats.
Please find attached the relevant documentation for reference:
I hope this helps.

カテゴリ

Help Center および File ExchangeData Import from MATLAB についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by