How to convert a structure into a individual double elements
Mostrar comentarios más antiguos
Hello all,
I have a structure containing ten "8760x1 double" I need to do some descriptive statistical analysis for each group of data. I want to convert this structure into ten dettached differents elements. How can I do this? I have tried struct2cell and I get one 10x1 cell but then I don't know how to dettach each group of data out of this.
Thanks for your help!
Respuesta aceptada
Más respuestas (6)
grapevine
el 25 de Jun. de 2012
there are different ways to sort out your issue
you might use:
C= struct2cell(s)
and then
A= cell2mat(C)
another way is to write a small code with a loop to copy all the elements of the structure in an array, for example
s = struct('f1', [1, 3, 2, 4])
a=[]
lenS=length(s.f1)
for i=1:1:lenS
a=[ a s(f1)]
end
good luck
Andrei Bobrov
el 25 de Jun. de 2012
Try this is code
A = struct2cell(yourstructdata);
out = cat(2,A{:});
3 comentarios
Eli
el 14 de Jul. de 2022
Very nice solution
burak enes
el 14 de Mzo. de 2023
super duber solution
Talha
el 28 de Dic. de 2023
best
Irwansyah Ramadhani
el 5 de Jun. de 2016
0 votos
I have a struct 2x1 and I want to write it's contain to excel directly. So, I want to ask the way to do it. Thank you
1 comentario
Image Analyst
el 5 de Jun. de 2016
Call xlswrite for each member of the structure. You have to get a numerical array or a cell array of strings. If you don't know how to do that then tell us what the structure of your structure is.
Mayss Aliyeh
el 19 de Mayo de 2017
0 votos
What if in the struct I have 7 fields, and all of them are different sizes of doubles? I want to use them for the svmtrain. How can I solve my problem?
Mark Brandon
el 5 de Dic. de 2017
0 votos
Consider a struct A, with fields a and b, each with a column of elements. You can convert the fields into column vectors using the following:
aVec = [A.a(:)] bVec = [A.b(:)]
Asi angel
el 23 de Mzo. de 2021
0 votos
Question
How to change struct 1x2 to double 1x1?
4 comentarios
Image Analyst
el 24 de Mzo. de 2021
You can't unless you throw away everything except for one scalar double value.
Merve Buyukbas
el 24 de Mzo. de 2021
Arrays of structures work the same as regular arrays. Use indexing to access individual items for change:

Image Analyst
el 24 de Mzo. de 2021
Yes, but note that s1 and s2 are still structures, not a double 1x1:
s = struct('f1', {5;6}, 'f2', {'aaa'; 'bbb'})
whos s
s1 = s(1)
s2 = s(2)
whos s1
s =
2×1 struct array with fields:
f1
f2
Name Size Bytes Class Attributes
s 2x1 572 struct
s1 =
struct with fields:
f1: 5
f2: 'aaa'
s2 =
struct with fields:
f1: 6
f2: 'bbb'
Name Size Bytes Class Attributes
s1 1x1 350 struct
Now if you wanted one of the doubles that is in one of the fields of one of the structures in the structure array, say the 5 from the f1 field of the first structure, you could do
% Pull out the 5 from the f1 field of the first structure in the array of structures.
scalarDouble = s(1).f1
whos scalarDouble
scalarDouble =
5
Name Size Bytes Class Attributes
scalarDouble 1x1 8 double
"How to change struct 1x2 to double 1x1?"
Clearly this is a MATLAB riddle, which is rather nice. One solution is:
'struct 1x2'
ans(:) = ans-[15,5,-3,19,-9,15,0,0,0,1]
Note how it changes the data, exactly as requested. CODY enthusiasts can add their own solutions.
Categorías
Más información sobre Structures en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!