필터 지우기
필터 지우기

Structure that contain an array of points with string names and coordinates

조회 수: 1 (최근 30일)
Hello,
I need to create and dynamically fill a structure that describes a point in space and which contains IDs (integers), coordinates [X, Y, Z] of type double and a name (string) label of each point. Labels can vary in length. Later I want to access and display all elements in the structure which corresponds to a certain ID.
This is what I manage to do so far.
% Define structure
points_data = struct('PointID', [], 'Coordinates', [], 'Names',[]);
% To have repeatable data
rng (0)
% Fill the data
for i = 1:10
% generate random coordinates
rand_coord = rand(1,3,'double');
points_data.PointID(end+1) = i;
points_data.Coordinates{end+1} = rand_coord;
points_data.Names{end+1} = ['Point', num2str(i)];
end
% Display all data with PointID 2
points_data{points_data.PointID==2}
But this code produce error if I try to access via PointID == 2. The only other way I can think of is to create a cell array so that each element in this cell array is Point structure.
What is your advice?

채택된 답변

Chunru
Chunru 2022년 10월 12일
Use array of structure
% Define structure
points_data = struct('PointID', [], 'Coordinates', [], 'Names',[]);
% To have repeatable data
rng (0)
% Fill the data
for i = 1:10
% generate random coordinates
rand_coord = rand(1,3,'double');
points_data(i).PointID = i;
points_data(i).Coordinates = rand_coord;
points_data(i).Names = ['Point', num2str(i)];
end
% Display all data with PointID 2
points_data
points_data = 1×10 struct array with fields:
PointID Coordinates Names

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by