MATLAB Answers

How to convert a structure into a individual double elements

673 ビュー (過去 30 日間)
Artu
Artu 2012 年 6 月 25 日
編集済み: Stephen 2021 年 3 月 25 日
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!

採用された回答

Image Analyst
Image Analyst 2012 年 6 月 25 日
Why not just simply do
vector1 = yourStruct.vector1;
vector2 = yourStruct.vector2;
vector3 = yourStruct.vector3;
vector4 = yourStruct.vector4;
vector5 = yourStruct.vector5;
vector6 = yourStruct.vector6;
vector7 = yourStruct.vector7;
vector8 = yourStruct.vector8;
vector9 = yourStruct.vector9;
vector10 = yourStruct.vector10;
which is very easy, intuitive, and straightforward?
  1 件のコメント
T Path
T Path 2015 年 12 月 12 日
I had a bit of a rough time loading data from a .mat file. This method works like a charm.

サインインしてコメントする。

その他の回答 (6 件)

grapevine
grapevine 2012 年 6 月 25 日
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
Andrei Bobrov 2012 年 6 月 25 日
Try this is code
A = struct2cell(yourstructdata);
out = cat(2,A{:});

Irwansyah Ramadhani
Irwansyah Ramadhani 2016 年 6 月 5 日
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 件のコメント
Image Analyst
Image Analyst 2016 年 6 月 5 日
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.
Also, you might read this: the FAQ

サインインしてコメントする。


Mayss Aliyeh
Mayss Aliyeh 2017 年 5 月 19 日
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
Mark Brandon 2017 年 12 月 5 日
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(:)]

Mohammad Basil
Mohammad Basil 2021 年 3 月 23 日
Question
How to change struct 1x2 to double 1x1?
  4 件のコメント
Stephen
Stephen 2021 年 3 月 24 日
"How to change struct 1x2 to double 1x1?"
Clearly this is a MATLAB riddle, which is rather nice. One solution is:
'struct 1x2'
ans = 'struct 1x2'
ans(:) = ans-[15,5,-3,19,-9,15,0,0,0,1]
ans = 'double 1x1'
Note how it changes the data, exactly as requested. CODY enthusiasts can add their own solutions.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by