Possible to create a function where the input is changed?
이전 댓글 표시
I understand it is one of the "cardinal rules" of MATLAB that functions do not affect the input values (versus scripts that do); however, is it possible to create a function where this is violated?
For example:
>>x = 10
>>Function(x)
%Running this would change the value of x in the workspace to some other value
채택된 답변
추가 답변 (2개)
Matt Tearle
2012년 2월 10일
I am not doing this...
classdef passbyref < handle
properties
value
end
methods
function x = passbyref(y)
x.value = y;
end
function notagreatidea(x)
x.value = x.value + 1;
end
end
end
And then
>> x = passbyref(42)
>> notagreatidea(x)
>> x
May Cleve have mercy on my soul.
Also: what Sean and Walter said.
댓글 수: 7
Josh
2012년 2월 10일
Sean de Wolski
2012년 2월 10일
Nope there are two functions and a class definition in there.
Walter Roberson
2012년 2월 10일
A class definition is not considered a function.
classdef need to go in to .m files with the same name as the class being defined, and "classdef" must be the first non-blank non-comment line, and nothing except whitespace and comments can go after the "end" of the classdef.
Josh
2012년 2월 10일
Sean de Wolski
2012년 2월 10일
no, you need just one file. You could ghave multiple files (for the methods) but then you'd need a special folder - not worth it for two one-line methods.
http://www.mathworks.com/help/techdoc/matlab_oop/ug_intropage.html
Matt Tearle
2012년 2월 10일
I'll let you argue the semantics with your bet opponent, but notagreatidea is kindasorta a function -- it's actually a method of the passbyref class. So it only works on passbyref objects. However, once you have a passbyref object (x = passbyref(42)), it is a single "function call" that invokes the notagreatidea method and changes its value property.
For the sake of your bet, follow Sean's first link... [hint]
Josh
2012년 2월 13일
Walter Roberson
2012년 2월 10일
0 개 추천
Yes, it is possible. There are not many cases where it is a good idea, however.
카테고리
도움말 센터 및 File Exchange에서 Tables에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!