Re: problems with persistence



REIDAR <aureid@xxxxxxxxx> writes:

To put it simply, my persistent variables aren't persisting (between
function calls)

I've attached my code, but I'll describe what is going wrong. When I
start up my program, I tell the function to 'clear'. This clears all
the values, and reinitializes everybody. Later on, when I try to
store a value, it doesn't have the variables in memory, so it
reinitializes them, wiping out the structure, and leading to a whole
pantload of error messages.

I understood that the whole point of persistant variables is that they
persist. What am I doing wrong?

My code has a little method for storing everything, it looks like
this:

function out=database(direction,name,value)
switch direction
case 'clear'
out=-1;
clear stacks names index
persistent stacks names index
stacks=struct('matrix',[]);
names=struct('string',[]);
index=0;
case 'return'
switch name
case 'stacks'
out=stacks;
return

Since persistent is in the 'clear' case, the 'return' case does not know
that the variables are supposed to be persistent.

Instead, put the "persistent" line at the top, before the "switch"
block. And there's no need to clear the variables for initialization.
Just directly set them to the initial values.

-Peter
.



Relevant Pages

  • Re: problems with persistence
    ... the values, and reinitializes everybody. ... switch direction ... persistent stacks names index ... I moved persistent outside of the switch block, bt I also had to move ...
    (comp.soft-sys.matlab)
  • Re: problems with persistence
    ... store a value, it doesn't have the variables in memory, so it ... I understood that the whole point of persistant variables is that they ... persistent stacks names index ...
    (comp.soft-sys.matlab)