Save arrays in a structure

29 views (last 30 days)
Abderrahmane DADA
Abderrahmane DADA on 15 May 2019
Commented: madhan ravi on 16 May 2019
Hi.
I have written an algorithm that computes an array "b" for each ith iteration [i={1,2,3,4}], and I would like to save those arrays in a structure called "A".
How to save those arrays in the structure "A" where its elements are function of "i", i.e, A = {b1, b2,b3, b4} and to get b1 for instance, one would have to type A.b1.
Regards.
  2 Comments
Stephen23
Stephen23 on 16 May 2019
Edited: Stephen23 on 16 May 2019
Note that using a non-scalar structure would be more efficient than generating fieldnames.
Indexing is much faster and less buggy than messing around forcing meta-data (i.e. that index) into some fieldnames.
madhan ravi
madhan ravi on 16 May 2019
Agree with Stephen.

Sign in to comment.

Accepted Answer

madhan ravi
madhan ravi on 15 May 2019
doc cell2struct
  3 Comments
madhan ravi
madhan ravi on 16 May 2019
Edited: madhan ravi on 16 May 2019
A=struct; % an example
for k=1:5
A.(sprintf('b%d',k))=rand(5);
end
Abderrahmane DADA
Abderrahmane DADA on 16 May 2019
Thanks a lot Madhan.
That's exactly what I was looking for.

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!