Facing problems in creating and indexing structured array

2 次查看(过去 30 天)
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