Facing problems in creating and indexing structured array
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Preyanka Dey
el 3 de Sept. de 2020
Comentada: Preyanka Dey
el 3 de Sept. de 2020
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 comentarios
Respuesta aceptada
David Hill
el 3 de Sept. de 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
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!