Writing "this" class' properties?

조회 수: 7 (최근 30일)
Eleftherios Ioannidis
Eleftherios Ioannidis 2011년 3월 22일
Hi, I am new to Matlab classes. I want to change the value of some properties in a function called ChangeProperties from within the class. But I can't do it. What am I doing wrong?
classdef agent_type
properties
v
id
end
methods
function p=agent_type(i)
p.id=i;
p.v=10*randn(1,2);
end
function y=ChangeProperties(this,step)
[this.v(step,:)]=zeros(1,2);
y=[this.v(step,:)];
end
end
end
when I run: agent.ChangeProperties(2) I get a random 1x2 vector. It should be a zero 1x2 vector. Why does this.v remain the same?
Thank you
  댓글 수: 2
Andrew Newell
Andrew Newell 2011년 3월 23일
I'm not sure what you're trying to do. Do you want to change your agent and also return a vector, or just change your object?
Andrew Newell
Andrew Newell 2011년 3월 23일
Also, what is step supposed to do? As written, it is adding rows when step > 1.

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

채택된 답변

Andrew Newell
Andrew Newell 2011년 3월 23일
I'm just guessing at what you're trying to do, but here is a different version of your method:
function y=ChangeProperties(this,vnew)
y = this;
y.v=vnew;
end
Then if you run
a = agent_type(1);
a = a.ChangeProperties([0 0])
you get
a =
agent_type
Properties:
v: [0 0]
id: 1
  댓글 수: 1
Eleftherios Ioannidis
Eleftherios Ioannidis 2011년 3월 23일
Great answer Andrew, as always. Thank you. Also, thank you for the pm.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Class Introspection and Metadata에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by