MATLAB Answers

Facing problems in creating and indexing structured array

1 view (last 30 days)
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

  0 Comments

Sign in to comment.

Accepted Answer

David Hill
David Hill on 3 Sep 2020
pts = struct('num',{},'x',{},'y',{});
for i=1:n
pts(i).num=i;
pts(i).x=long(i);
pts(i).y=long(i);
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!