필터 지우기
필터 지우기

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