Object array: modify properties of a single element
显示 更早的评论
I'll try to be short.
I have the class "testClass" defined in this way:
classdef testClass<handle
properties
value=[];
end
methods
function modifyValue(input,number)
input.value=number;
end
end
end
Then, in the command window I write:
>>MyClass(1:10)=testClass;
>>MyClass(1).modifyValue(10);
The idea is to create an array of objects (made of, in this case, 10 elements). With the second line command I'd like to modify only the property ("value") of the first element of the array "MyClass", leaving the properties of the other elements "MyClass(2:end)" unchanged.
I get the following:
>> MyClass(1)
ans =
testClass with properties:
value: 10
>> MyClass(2)
ans =
testClass with properties:
value: 10
[...]
the property "value" is set equal to 10 for all the elements of the array "MyClass". How can make it work in the intended way? (e.g. "MyClass(1)" having a property "value" equal to 10 while "MyClass(2)" equal to [])
Thank you for your help and sorry for my bad english.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File 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!