is it possible to create array of strings or char from struct of cell arrays

1 view (last 30 days)
SBird
SBird on 20 Apr 2021
Edited: Stephen on 21 Apr 2021
Apologies if I get some of the terminology wrong. I have a struct which is an array, with some cell values. So:
A(1).name='one', A(1).val=1, A(2).name='two', A(2).val=2.
What I'd like to do is create an array of strings from just the names. So, I'd like B=['one','two'].
if I do B=A(:).name I get B='onetwo'.
All help appreciated!
  1 Comment
Stephen
Stephen on 21 Apr 2021
These resources show one common approach:
"if I do B=A(:).name I get B='onetwo'."
A(1).name='one'; A(1).val=1; A(2).name='two'; A(2).val=2;
B=A(:).name
B = 'one'
I do not see how you could get 'onetwo' using that code.

Sign in to comment.

Accepted Answer

per isakson
per isakson on 21 Apr 2021
Edited: per isakson on 21 Apr 2021
There are character arrays (See Text and Characters)
A(1).name='one'; A(1).val=1; A(2).name='two'; A(2).val=2;
{A.name}
ans = 1×2 cell array
{'one'} {'two'}
[A.name]
ans = 'onetwo'
and there are string arrays
A(1).name="one"; A(1).val=1; A(2).name="two"; A(2).val=2;
[A.name]
ans = 1×2 string array
"one" "two"

More Answers (0)

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!