Effacer les filtres
Effacer les filtres

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

8 vues (au cours des 30 derniers jours)
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."

Réponse acceptée

Matt J
Matt J le 13 Oct 2022
Modifié(e) : Matt J le 13 Oct 2022
% 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"

Plus de réponses (0)

Catégories

En savoir plus sur Construct and Work with Object Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by