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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by