Setting a Value in an Array in a Class

classdef myclass_t
properties
my_array double
end
methods
function self = myclass_t( array_size )
self.my_array = zeros(1, array_size);
end %constructor
function self = set.my_array(self, my_val, my_index)
self.my_array(my_index) = my_value;
end
end
end
I want to set the value of my array with the value my_value at my_index. Lets assume there are no type errors.
However when I do this, I get the following error: Set Methods must have exactly two inputs
How can I set the value of this array at the desired index?

댓글 수: 3

Matt J
Matt J 2019년 9월 19일
What syntax are you trying to enable with this?
A. B.
A. B. 2019년 9월 19일
I don't know. I just want to set the damn value lol
What's wrong with simply doing a direct assignment to my_array all the time,
obj=myclass_t(10);
obj.my_array(5)=6;

댓글을 달려면 로그인하십시오.

답변 (1개)

Matt J
Matt J 2019년 9월 19일
편집: Matt J 2019년 9월 19일

1 개 추천

Use an ordinary method:
classdef myclass_t
properties
my_array double
end
methods
function self = myclass_t( array_size )
self.my_array = zeros(1, array_size);
end %constructor
function self = set_my_array(self, my_val, my_index) %<---changed set. to set_
self.my_array(my_index) = my_value;
end
end
end

댓글 수: 4

A. B.
A. B. 2019년 9월 19일
When I get back from that function call however, nothing has changed.
Matt J
Matt J 2019년 9월 19일
편집: Matt J 2019년 9월 19일
You must call it with an output argument,
obj = obj.set_my_array(my_val, my_index)
Alternatively, you can write this as a handle class,
classdef myclass_t < handle %<---NOTICE
properties
my_array double
end
methods
function self = myclass_t( array_size )
self.my_array = zeros(1, array_size);
end %constructor
function set_my_array(self, my_val, my_index) %<---NOTICE
self.my_array(my_index) = my_value;
end
end
end
but make sure you know how handle classes work, and how they behave differently from value classes:
Careful there! That set method for the handle class is not valid.
Property set methods do not support indexing into the property. You have to replace the whole array.
So with the value class, a valid set method would be
function self = set.my_array(self, value)
self.my_array = value;
end
and for a handle class, the equivalent:
function set.my_array(self, value)
self.my_array = value;
end
Note that in both case, you never call the set method directly. It's invoked for you by matlab when you do:
obj.my_array = [1, 2, 3]; %will invoke set.my_array(obj, [1, 2, 3]) if the method is defined
With this particular example, the set method is completely pointless. It's useful if you want to do additional validation or something more complex than just assigning the input to the property.
If you do want to index into the property, then either don't define a set method so you can access directy the public property, or create a normal method as per matt's first example.
Matt J
Matt J 2019년 9월 19일
Yep. I've made the appropriate edits.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Use Prebuilt MATLAB Interface to C++ Library에 대해 자세히 알아보기

제품

릴리스

R2018b

질문:

2019년 9월 19일

댓글:

2019년 9월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by