How can I assign a set of data from a 'meta.class' object propertys to a string array without using a loop statement?

5 Ansichten (letzte 30 Tage)
When I run 'mc=?ClassA' in command line, I got a meta.class object mc. mc.MethodList.Name store all the motheds of ClassA
I run 'K=[mc.MethodList(:,1).Name]' in command line,I got :
K = 'ClassAemptyeqneltgtlegedeleteisvalidfindpropnotifynotifyaddlistenerlisteneraddlistenerlisteneraddlistenerlisteneraddlistenerlisteneraddlistenerlistenerfindobj'
I want to separate the 24 concatenated "method names." like this:
K=['ClassA' 'empty' …………]
I've tried several methods, and only the following approach works, but it requires a lot of code. I'd like to know if there's a simpler way to achieve the same result?
b=cell(1,24);
[b{1,:}]=deal('');
[b{1,1},b{1,2}]=deal(mc.MethodList(1,1).Name,mc.MethodList(2,1).Name);
b{1,1}
ans = 'ClassA'
b{1,2}
ans = 'empty'
If you want to complete all variable assignments, you need the following code:
[b{1,1},b{1,2},…………]=deal(mc.MethodList(1,1).Name,mc.MethodList(2,1).Name,…………);
ClassA.m
classdef ClassA < handle
properties (SetObservable) %或(SetObservable=true)
x=1;
y=2;
end
events
kk
end
methods
function obj=ClassA()
x=1;
end
end
end

Akzeptierte Antwort

Bruno Luong
Bruno Luong am 20 Aug. 2023
Bearbeitet: Bruno Luong am 20 Aug. 2023
cellarray... not array
mc=?ClassA;
K={mc.MethodList.Name}
K = 1×24 cell array
Columns 1 through 15 {'ClassA'} {'empty'} {'eq'} {'ne'} {'lt'} {'gt'} {'le'} {'ge'} {'delete'} {'isvalid'} {'findprop'} {'notify'} {'notify'} {'addlistener'} {'listener'} Columns 16 through 24 {'addlistener'} {'listener'} {'addlistener'} {'listener'} {'addlistener'} {'listener'} {'addlistener'} {'listener'} {'findobj'}
%or if prefer
M=string(K)
M = 1×24 string array
Columns 1 through 17 "ClassA" "empty" "eq" "ne" "lt" "gt" "le" "ge" "delete" "isvalid" "findprop" "notify" "notify" "addlistener" "listener" "addlistener" "listener" Columns 18 through 24 "addlistener" "listener" "addlistener" "listener" "addlistener" "listener" "findobj"

Weitere Antworten (0)

Kategorien

Mehr zu Testing Frameworks finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by