Convert char fields of structure into a cell array

I have a structure with fields like
A(1).a='A01'
A(2).a='A02'
A(3).a='A03'
Now I would like to convert this structure into a cell array. I tried with
B=[A.a]
B =
A01A02A03
That is not, what I need. I would like to have:
B =
'A01'
'A02'
'A03'
Is there an easy way, or do I need to write a loop for that?

 Akzeptierte Antwort

Sean de Wolski
Sean de Wolski am 21 Feb. 2014
Bearbeitet: Sean de Wolski am 21 Feb. 2014
You could also use comma-separted list expansion:
ac = {A(:).a}
Similar to what you did above, but rather than concatenating the strings with [], concatenate them into cells with {}.

5 Kommentare

Jesus! Thanks. You don't know how much frustrated I was trying to get this working. What a nightmare!
Isn't there any good manual where explains differences between arrays, cells and structs? The use of [], (), and {} can be sometimes misleading.
Hi Dani,
For cell arrays, probably this:
The higher level page that leads to pages like the above would be this one:
Clare
Clare am 1 Jul. 2015
Hey thanks! I've been looking for a solution like this as well.
Marie-Claude Boisvert
Marie-Claude Boisvert am 7 Sep. 2017
Bearbeitet: Marie-Claude Boisvert am 7 Sep. 2017
Hi, I tried it and got a new string vector :) super , but do you know how to change it directly in the Structure (A here) ? Thanks
as a
as a am 22 Apr. 2021
I think what u need is B = char(A.a)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 21 Feb. 2014

Kommentiert:

am 22 Apr. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by