cellData = {'Name', 'pos_x', 'pos_y'; 'Harry', 34, 2; 'Bernard', 5, 11}
Then
for col = 1:size(cellData ,2)
fieldName = cellData{1, col};
c = cellData (2:end, col);
[point(1:length(c)).(fieldName)] = c{:};
end
Now
point(1)
ans =
Name: 'Harry'
pos_x: 34
pos_y: 2
point(2)
ans =
Name: 'Bernard'
pos_x: 5
pos_y: 11
EDIT:
If you want to only have 2 fields, i.e. Name
and posxyz
then:
[M, N] = size(celldata);
names = celldata(:,1);
[point(1:M).Name] = names{:};
xyz = mat2cell(cell2mat(celldata(:,2:end)), ones(M,1), N - 1);
[point(1:M).posxyz] = xyz{:};
now for celldata={'Point1' .1 .1 .2;'Point2' 0 0 .1;'Middle' .1 .2 .1;'Point3' 0 1 0}
point(1)
ans =
Name: 'Point1'
posxyz: [0.1000 0.1000 0.2000]
point(2)
ans =
Name: 'Point2'
posxyz: [0 0 0.1000]