Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Persistent variable initial values

Subject: Persistent variable initial values

From: Jeff

Date: 4 Dec, 2009 23:40:04

Message: 1 of 3

Hi, I am modifying a blackjack program (this one: http://www.mathworks.com/moler/ncm/blackjack.m) for a Math Computing project. The command

persistent deck ncards

is setting deck to a <1x1028 double> and ncards to 33. These variables have not yet been set anywhere and according to the help:

If the persistent variable does not exist the first time you issue the persistent statement, it is initialized to the empty matrix.

So why is persistent setting deck and ncards to these values? I am at a loss to figure this out. Thanks for any help,
-Jeff

Subject: Persistent variable initial values

From: Jeff

Date: 4 Dec, 2009 23:57:03

Message: 2 of 3

"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hfc6kk$5j9$1@fred.mathworks.com>...
> Hi, I am modifying a blackjack program (this one: http://www.mathworks.com/moler/ncm/blackjack.m) for a Math Computing project. The command
>
> persistent deck ncards
>
> is setting deck to a <1x1028 double> and ncards to 33. These variables have not yet been set anywhere and according to the help:
>
> If the persistent variable does not exist the first time you issue the persistent statement, it is initialized to the empty matrix.
>
> So why is persistent setting deck and ncards to these values? I am at a loss to figure this out. Thanks for any help,
> -Jeff

Correction: The initial value of deck is a 1x208 double. However, this is still not what I would expect, based on the help file. Where is it getting these values from?

Subject: Persistent variable initial values

From: Jeff

Date: 5 Dec, 2009 00:45:21

Message: 3 of 3

"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hfc7kf$6hu$1@fred.mathworks.com>...
> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hfc6kk$5j9$1@fred.mathworks.com>...
> > Hi, I am modifying a blackjack program (this one: http://www.mathworks.com/moler/ncm/blackjack.m) for a Math Computing project. The command
> >
> > persistent deck ncards
> >
> > is setting deck to a <1x1028 double> and ncards to 33. These variables have not yet been set anywhere and according to the help:
> >
> > If the persistent variable does not exist the first time you issue the persistent statement, it is initialized to the empty matrix.
> >
> > So why is persistent setting deck and ncards to these values? I am at a loss to figure this out. Thanks for any help,
> > -Jeff
>
> Correction: The initial value of deck is a 1x208 double. However, this is still not what I would expect, based on the help file. Where is it getting these values from?

OK, sorry to be posting so many times in a row, but I see some relevant lines of code. However, they seem to set deck and ncards to different values (at least ncards, anyway) than the persistent command is returning. Here is the entire function:

function c = deal
% Deal one card

    persistent deck ncards
    if isempty(deck) | ncards < 6
       % Four decks
       deck = [1:52 1:52 1:52 1:52];
       % Shuffle
       ncards = length(deck);
       deck = deck(randperm(ncards));
    end
    c = deck(ncards);
    ncards = ncards - 1;
end

So deck is set to a vector of length 208, which is consistent with what the persistent command returns (even if it's not quite consistent with the help). But the first time through, shouldn't ncards be set to 208? It is being set to 33. Why?

Again, thanks to whoever can explain,
-Jeff

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us