From: David on
Hi all,

I have a question regarding setting the state of RandStream:

If I want to set the state of the default stream to "mystate", I have to do the following:
ds=RandStream.getDefaultStream;
ds.State=mystate;

My question is why can't I just execute this:
RandStream.getDefaultStream.State=mystate

I tried the line above and it corrupts RandStream; it causes problems with legit operations such as RandStream creation such as bob=RandStream.create('mt19937ar','Seed',10) ).

Thanks,
David
From: Peter Perkins on
On 3/29/2010 2:44 AM, David wrote:

> If I want to set the state of the default stream to "mystate", I have to
> do the following:
> ds=RandStream.getDefaultStream;
> ds.State=mystate;
>
> My question is why can't I just execute this:
> RandStream.getDefaultStream.State=mystate

David, you have to remember that getDefaultState is a method. You can call it without parentheses (because it takes no input arguments), but really what you're doing is

RandStream.getDefaultStream().State=mystate

MATLAB does not support directly subscripting on the result of a function or method call. In this case what you've done is being interpreted as creation of a structure within a newly-created structure, which is legal.

> I tried the line above and it corrupts RandStream; it causes problems
> with legit operations such as RandStream creation such as
> bob=RandStream.create('mt19937ar','Seed',10) ).

What happens in this case is that a scalar structure named RandStream gets created. It has one field, named getDefaultStream, which itself has one field. named State. That scalar structure hides the class.

Hope this helps.