Accessing data within astructure

Hello,
I am trying to access data from a part of a structure, which I have not implemented myself.
It looks like:
>> whos
Name Size Bytes Class Attributes
anotace 1x1 2648 struct
then:
anotace.borders
ans =
1x5 struct array with fields:
Y
H
type
>> anotace.borders.Y
ans =
106.0000 106.0000 164.5000 164.5000
685.2500 733.2500 733.2500 685.2500
ans =
368.5000 368.5000 440.5000 440.5000
604.2500 661.2500 661.2500 604.2500
ans =
351.5448 351.5448 421.5945 421.5945
461.3955 521.8930 521.8930 461.3955
ans =
356.3209 356.3209 432.7388 432.7388
306.9677 369.0572 369.0572 306.9677
ans =
337.2164 337.2164 423.1866 423.1866
192.3408 252.8383 252.8383 192.3408
I would like to access anotace.borders.Y and use its data as an array of arrays(each ans as one array). When I try to assign its value I only get the first ans into my new variable.
Thanks for help.

回答 (2 件)

Amit
Amit 2014 年 1 月 19 日

0 投票

you can access each array as anotace.borders.Y{i} where i is the i'th array in the structure.

4 件のコメント

Amit
Amit 2014 年 1 月 19 日
Also, lets say you're try to create variables in a sequence like val1, val2 and blah ..
you can do something like
for ii = length(anotace.borders.Y)
eval(['val' num2str(ii) '=anotace.borders.Y{' num2str(ii) '}']);
end
Jan
Jan 2014 年 1 月 19 日
Thanks for answer, I tried it, but I get:
>> anotace.borders.Y{1}
Field reference for multiple structure elements that is followed by more
reference blocks is an error.
Also somewhere Matlab stated that my anotace.borders.Y is type 'double' which I don´t understand in respect to the output I have posted above.
Amit
Amit 2014 年 1 月 19 日
編集済み: Amit 2014 年 1 月 19 日
My bad. anotace.borders is the struct with fields Y. Try this. This will work.
for ii = 1:5
eval(['val' num2str(ii) '=anotace.borders(' num2str(ii) ').Y']);
end
Jan
Jan 2014 年 1 月 19 日
@Amit: Using EVAL to create variables dynamically is a very bad programming practize and there is always a better solution.
Jan
Jan 2014 年 1 月 19 日

0 投票

data = {anotace.borders.Y};
data{1}
data{2}

この質問は閉じられています。

質問済み:

Jan
2014 年 1 月 19 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by