Create a 1 x m Structure instead of a 1x1 Structure

39 visualizaciones (últimos 30 días)
Ruger28
Ruger28 el 8 de Mayo de 2020
Comentada: Ruger28 el 8 de Mayo de 2020
Hello,
Im tring to create a 1 x m structure instead of a 1x1. When doing the following, it creates a 1x1 struct
MySturcture.FirstField = [1:100]
MySturcture.SecondField = [1:100]
I would like to open the struct to the fields being side by side (1x2 structure), displaying all of the data, similar to the image in this question.
[FirstField] [SecondField]
1 1
2 2
3 3
etc.. etc..
Any help would be greatly appreciated.

Respuesta aceptada

Stephen23
Stephen23 el 8 de Mayo de 2020
S = struct('FirstField',num2cell(1:100), 'SecondField',num2cell(1:100))
  1 comentario
Ruger28
Ruger28 el 8 de Mayo de 2020
Thanks, Stephen! This is exactly what I was looking for. Kind of confused why they have to be cells instead of a double, but definitely what I needed. Thanks again.

Iniciar sesión para comentar.

Más respuestas (2)

Ameer Hamza
Ameer Hamza el 8 de Mayo de 2020
Editada: Ameer Hamza el 8 de Mayo de 2020
This is one of the way.
MySturcture(100).FirstField = [];
MySturcture(100).SecondField = [];
A = num2cell(1:100);
[MySturcture.FirstField] = A{:};
[MySturcture.SecondField] = A{:};
  1 comentario
Ruger28
Ruger28 el 8 de Mayo de 2020
Thank you kindly, but Stephen's answer was a little simplier to implement. Thanks, though!

Iniciar sesión para comentar.


Steven Lord
Steven Lord el 8 de Mayo de 2020
Your second design looks less like a struct and more like a table.
x = (0:11).';
y = x.^2;
t = table(x, y)
If you wanted a new "field" (table variable) you can add them, using variables from the table and/or variables from the workspace as desired.
t.z = t.y - t.x/2
t.w = sinpi(x./4)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by