Re: problems with persistence
- From: "Titus" <titus.edelhofer@xxxxxxxxxxxx>
- Date: Wed, 30 Apr 2008 15:38:53 +0200
"REIDAR" <aureid@xxxxxxxxx> schrieb im Newsbeitrag
news:ec8e5922-1146-446c-b8ed-b7a72dfb19ef@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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
case 'names'
out=names;
return
case 'index'
out=index;
return
end
case 'store'
out=0;
index=index+=1;
switch name
case 'stacks'
stacks(index).matrix=value;
return
case 'names'
names(index).matrix=value;
return
case 'index'
index=value;
return
end
end
Hi,
move the persistent to the front:
function out=database(direction,name,value)
persistent stacks names index
switch direction
case 'clear'
out=-1;
stacks=struct('matrix',[]);
names=struct('string',[]);
index=0;
case 'return'
....
BTW: for such things I would take a look at nested functions. Especially if
you need to have more than one database...
Titus
.
- References:
- problems with persistence
- From: REIDAR
- problems with persistence
- Prev by Date: Re: problems with persistence
- Next by Date: GINPUT and PAUSE bug
- Previous by thread: Re: problems with persistence
- Next by thread: inverse Helmholtz solver
- Index(es):
Relevant Pages
|