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

 採用された回答

Sean de Wolski
Sean de Wolski 2012 年 2 月 10 日

0 投票

doc assignin
doc evalin
Why would you want to do this?

6 件のコメント

Josh
Josh 2012 年 2 月 10 日
How would I use these if I wanted to, say, increase the value of x from 1 to 2?
It's more conceptual. To be honest, you could say I made a bet with someone.
Sean de Wolski
Sean de Wolski 2012 年 2 月 10 日
Read the documentation I linked to. I'll only give you more details if you cut me in on your winnings!
Josh
Josh 2012 年 2 月 10 日
Oh, that's the command for documentation. I'll be sure to check it out, thanks!
Walter Roberson
Walter Roberson 2012 年 2 月 10 日
If the bet is just whether it can be done or not, then the answer is Yes, it can. If the question is whether _you_ can come up with a way to do it, then us telling you the mechanism would probably violate the terms of the bet.
We'd _prefer_ not to describe the details. The question is sort of like asking a professional electrician about using coins to replace fuses: the answer is going to be, "Don't Do That!"
Josh
Josh 2012 年 2 月 10 日
It was more a bet of whether or not you can or cannot do it. I'll be sure to try and figure it out using that documentation; worst case scenario I'll use Matt's idea.
Josh
Josh 2012 年 2 月 13 日
Was able to create a function to do just this using assignin, thank you!

サインインしてコメントする。

その他の回答 (2 件)

Matt Tearle
Matt Tearle 2012 年 2 月 10 日

1 投票

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
Josh 2012 年 2 月 10 日
Is this all one function? (excluding the three lines at the bottom)
Sean de Wolski
Sean de Wolski 2012 年 2 月 10 日
Nope there are two functions and a class definition in there.
Walter Roberson
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
Josh 2012 年 2 月 10 日
I should have been more specific: should this all be in one file, or must I create seperate files for passbyref and notagreatidea?
Sean de Wolski
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
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
Josh 2012 年 2 月 13 日
Thanks for the help, but went with Sean's answer for simplicity's sake.

サインインしてコメントする。

Walter Roberson
Walter Roberson 2012 年 2 月 10 日

0 投票

Yes, it is possible. There are not many cases where it is a good idea, however.

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

製品

質問済み:

2012 年 2 月 10 日

編集済み:

2013 年 10 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by