Display strings and variable - cell array
古いコメントを表示
I want to disply the output of my variable. so i created the following code.
food= { 'Rice','Quinoa','Tortilla','Lentils','Broccoli' }';
X % X is the output
for n =1:length(X)
disp(food{X(ii)})
end
The code is not working. Any help will be appreciated.
回答 (1 件)
ANKUR KUMAR
2021 年 3 月 15 日
編集済み: ANKUR KUMAR
2021 年 3 月 15 日
Access the contents of cells by indexing with curly braces, {}.
food= { 'Rice','Quinoa','Tortilla','Lentils','Broccoli' }';
for n =1:length(food)
disp(food{n})
end
5 件のコメント
Telema Harry
2021 年 3 月 15 日
ANKUR KUMAR
2021 年 3 月 15 日
It is not necessary to assign X to food.
If you have specific indices in X to choose from food variable, you can use this chunk of code
food= { 'Rice','Quinoa','Tortilla','Lentils','Broccoli' }
X=[2,5]
for n =1:length(X)
disp(food{X(n)})
end
Telema Harry
2021 年 3 月 15 日
ANKUR KUMAR
2021 年 3 月 15 日
Please clear all the pre defined variables before running the code.
Index cannot be float. It must be an integer.
Below is the working complete code.
clc
clear
food= { 'Rice','Quinoa','Tortilla','Lentils','Broccoli' }
X=[2,5]
for n =1:length(X)
disp(food{X(n)})
end
Telema Harry
2021 年 3 月 17 日
カテゴリ
ヘルプ センター および File Exchange で Statistics and Machine Learning Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
