How to call data from a structure array?

Hi, I need help.
I need to get these results [using the calls: experiments, experiments(2), and experiments(1).height]:
>> experiments
experiments =
1x2 struct array with fields:
num
code
weights
height
>> experiments(2)
ans =
num: 11
code = ‘t’
weights: [111.4500 111.1100]
height: [1x1 struct]
>> experiments(1).height
ans =
feet: 5
inches: 6
I have this code, but I really don't know how to call and display the results above, because using the calls just lead to errors. What must be missing in my code?
experiments(1).num = 33;
experiments(1).code = 'x';
experiments(1).weights.1 = 200.34;
experiments(1).weights.2 = 202.45;
experiments(1).height.feet = 5;
experiments(1).height.inches = 6;
experiments(2).num = 11;
experiments(2).code = 't';
experiments(2).weights.1 = 111.45;
experiments(2).weights.2 = 111.11;
experiments(2).height.feet = 7;
experiments(2).height.inches = 2;
I also tried this kind of arrangement/code...
experiments(1) = struct('num',33,'code','x','weights',struct('1',200.34,'2',202.45),'height',struct('feet',5,'inches',6))
experiments(2) = struct('num',11,'code','t','weights',struct('1',111.45,'2',111.11),'height',struct('feet',7,'inches',2))
Your assistance will be greatly appreciated. Thanks in advance!

 採用された回答

Arthur Roué
Arthur Roué 2020 年 7 月 22 日

1 投票

You cannot name a field with just numbers
% This is not a correct syntax
experiments(2).weights.1 = 111.45;
experiments(2).weights.2 = 111.11;
Fields, like variables in MATLAB, must start with a letter.
experiments(2).weights.W1 = 111.45;
experiments(2).weights.W2 = 111.11;

7 件のコメント

Roxanne Esguerra
Roxanne Esguerra 2020 年 7 月 22 日
Oh, thank you. But will that correspond to this,
Roxanne Esguerra
Roxanne Esguerra 2020 年 7 月 22 日
And what should I do with calling the functions to get those results? Thanks!
Arthur Roué
Arthur Roué 2020 年 7 月 22 日
You have a strcuture-array experiments with 2 elements.
In this structure fields weights and height are also strcuture with respective fields W1, W2 (1 and 2 are not correct field names) and feet, inches. so yes !
Arthur Roué
Arthur Roué 2020 年 7 月 22 日
You access the value in a structure same way you create it with dot indexing.
experiments(2).height.feet
>> 7
Roxanne Esguerra
Roxanne Esguerra 2020 年 7 月 22 日
Thank you so much. But one more thing, when I try to call >>experiments, it results to nothing. However I must get this,
experiments =
1x2 struct array with fields:
num
code
weights
height
Arthur Roué
Arthur Roué 2020 年 7 月 22 日
experiments is a structure and not a function.
When you write
experiments
in the command windows, it displays the content of experiments. As experiments is a strcture with 2 element, it only displays its fields.
Use functions disp or fprintf(1, 'my str') to display a field value in the command window
Roxanne Esguerra
Roxanne Esguerra 2020 年 7 月 23 日
Thanks! I've figured it out. The script must be run first before typing the needed commands. Thank you so much :)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by