How to extract property values from an object array and put it into a new array?

8 次查看(过去 30 天)
classdef Datatest
properties (SetAccess = 'public' ,GetAccess = 'public')
Name
end
end
% creating 4 objects of the class 'Datatest'
x1 = Datatest();
x2 = Datatest();
x3 = Datatest();
x4 = Datatest();
% Changing the name of each objects property
x1.Name = 'test1';
x2.Name = 'test2';
x3.Name = 'test3';
x4.Name = 'test4';
% array of objects
list = [x1 x2 x3 x4]
dropDown = zeros(1,4)
for i=1:4
dropDown(i) = list(i).Name
end
i would like to create an array called 'dropDown' and extract all the names of objects from the object list. I tried this code which is giving me an arror "Unable to perform assignment because the left and right sides have a different number of elements."

采纳的回答

Matt J
Matt J 2022-10-13
编辑:Matt J 2022-10-13
% Changing the name of each objects property
x1.Name = 'test1';
x2.Name = 'test2';
x3.Name = 'test3';
x4.Name = 'test4';
list = [x1 x2 x3 x4];
dropDown=string({list.Name})
dropDown = 1×4 string array
"test1" "test2" "test3" "test4"

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by