Referencing String in an array?
2 次查看(过去 30 天)
显示 更早的评论
Hi guys, I am currently having the following problem
I want to create an array say:
A = ['Apple', 'Oranges']
And I want to access the array by the index of the items:
A(1) = Apple
A(2) = Oranges
But current, A(1) is only giving me a, A(2) is giving me p, so on
How do I access these items by their indices?
1 个评论
Azzi Abdelmalek
2013-9-29
Bo commented
Thanks guys, been out of the matlab game for over a year and I can't remember anything! I even tried to use a struct but matlab struct is kind of crazy also.
回答(2 个)
Azzi Abdelmalek
2013-9-29
Use a cell array
A = {'Apple', 'Oranges'}
A{1}
A{2}
1 个评论
Azzi Abdelmalek
2013-9-29
编辑:Azzi Abdelmalek
2013-9-29
Bo, this is not struct it's a cell array. When you work with different type of data, you can not avoid cell array
doc cell
Wayne King
2013-9-29
编辑:Wayne King
2013-9-29
Why not use a cell array?
A = {'Apple', 'Oranges'};
Then
A{1}
and
A{2}
will give you what you want.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!