Facing problems in creating and indexing structured array

1 回表示 (過去 30 日間)
Preyanka Dey
Preyanka Dey 2020 年 9 月 3 日
コメント済み: Preyanka Dey 2020 年 9 月 3 日
Hi everyone,
I am trying to create an structured arrays with fields 'num', 'x' and 'y'. 'num' is the iteration number, x and y are respectively the long and lat values I created before. I want the structure in a way so that when I will write
pts(1) % it will return the following
num: 1
x: 2.1376 % first element of long
y: 1.5334 % first value of lat
pts(2) =
num: 2
x: 1.8376 % 2nd element of long
y: 1.0044 % 2nd element of lat
However the code is not working. Can anybody please help me to solve this out? Thanks a lot. Following is my code.......
function main
n = 6;
long_min = 1.;
lat_min = 1.;
w = 2.;
h = 1.;
bound.xmin = long_min;
bound.xmax = long_min + w;
bound.ymin = lat_min;
bound.ymax = lat_min + h;
% generating the sample points
long = long_min + w * rand(1,n);
lat = lat_min + h * rand(1,n);
%structure arrays
for i = 1:n
pts(i) = struct('num', {'i'}, 'x', {'long(i)'}, 'y', {'lat(i)'});
end
end

採用された回答

David Hill
David Hill 2020 年 9 月 3 日
pts = struct('num',{},'x',{},'y',{});
for i=1:n
pts(i).num=i;
pts(i).x=long(i);
pts(i).y=long(i);
end
  1 件のコメント
Preyanka Dey
Preyanka Dey 2020 年 9 月 3 日
@David Hill.. It's working. Thanks a lot.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by