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

조회 수: 54 (최근 30일)
Ruger28
Ruger28 2020년 5월 8일
댓글: Ruger28 2020년 5월 8일
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.

채택된 답변

Stephen23
Stephen23 2020년 5월 8일
S = struct('FirstField',num2cell(1:100), 'SecondField',num2cell(1:100))
  댓글 수: 1
Ruger28
Ruger28 2020년 5월 8일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Ameer Hamza
Ameer Hamza 2020년 5월 8일
편집: Ameer Hamza 2020년 5월 8일
This is one of the way.
MySturcture(100).FirstField = [];
MySturcture(100).SecondField = [];
A = num2cell(1:100);
[MySturcture.FirstField] = A{:};
[MySturcture.SecondField] = A{:};
  댓글 수: 1
Ruger28
Ruger28 2020년 5월 8일
Thank you kindly, but Stephen's answer was a little simplier to implement. Thanks, though!

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2020년 5월 8일
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)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by