Create an array of structures

176 Ansichten (letzte 30 Tage)
Izan Segarra
Izan Segarra am 11 Okt. 2020
Beantwortet: Steven Lord am 11 Okt. 2020
Hello!
I'm trying to create a 4x5 matrix containing a 1x3 structure in each of its cells but I can't get the result shown in the picture. I only managed to get a 9x1 structure.
position = {'corner';'edge';'corner';'edge';'inside';'edge';'corner';'edge';'corner'};
temperature = {25;25;25;25;50;25;25;25;25};
ic = {0;0;0;0;0;0;0;0;0};
PCB = [position, temperature ,ic]
cell2struct(PCB,{'position';'temperature';'ic'},1)
Does anyone know how to create it? Thank you very much!
  4 Kommentare
Stephen23
Stephen23 am 11 Okt. 2020
"What would be the correct way to carry out the structure of 4x5?"
That depends on the format of the input data:
  • Are they generated one-at-a-time in a loop (or similar), or are they available in arrays?
  • Can the data arrays (if in arrays) change size? What size do the arrays use?
Possibly struct or one of the conversion functions would suit your task.
Izan Segarra
Izan Segarra am 11 Okt. 2020
The data are available in fixed-size arrays as follows:
- position: (4x5)
- temperature: (4x5)
- ic: (4x5)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Steven Lord
Steven Lord am 11 Okt. 2020
If the field values you pass into the struct function are a cell array, MATLAB will make a struct array the same size as the cell array. Each element of the struct will contain the data from the corresponding cell of the cell array.
A = {1 2 3; 4 5 6};
B = {'apple', 'banana', 'banana'; 'cherry', 'cherry', 'apple'};
S = struct('number', A, 'fruit', B);
T = S(2, 2) % T.number is 5, T.fruit is 'cherry'
See the second and third items in the Description section of the documentation page for the struct function for more information.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by