Function - Global variable in Matlab that retains an old value and won39t change

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I have created a function in Matlab and use the following run.m file to run it.

global kanasta
EEG_multitaper_autocor_SO_maheen_final(ALLEEG_LANCE_POST, All , Cz-CPz ,3,1,1,1)
POST=kanasta;

where kanasta is a variable generated from the function EEG_multitaper_autocor_SO_maheen_final and ALLEEG_LANCE_POST is a data structure.

From whatever kanasta (which is an array) I get from running this, I store it in POST as shown.

Then I change the code so that I run the following:

global kanasta
EEG_multitaper_autocor_SO_maheen_final(ALLEEG_LANCE_PRE, All , Cz-CPz ,3,1,1,1)
PRE=kanasta;

where I am using ALLEEG_LANCE_PRE is also a data structure with the same structure but just different data, everything else is exactly the same as ALLEEG_LANCE_POST.

I find that when I do this, there is some pre-stored version of kanasta that is incorrect and both PRE and POST turn out to be exactly the same thing. I am guessing its some sort of problem with the global kanasta which is declared global in my run.m file as well as the function as well. I am not sure why this is happening (it was fine before... my Matlab crashed last night and after that this has happened). And its messing up any further analysis I need to do on PRE and POST. I have also tried to save PRE to another variable and then running the code for POST and saving that, but it doesn t seem to work. I have cleared the workspace multiple times and cleared all variables and command history and restarted, but nothing works.

Does anybody have any advice on "resetting" perhaps? Or about global variables or why this might be happening?

Answers

In Matlab global variables really are global, not just global in one script or program. They are not exactly global for matlab, but unless cleared they will keep their value until the Matlab session ends. The solution is to write:

clear global;

when you want to clear the global variables. However, in older versions of matlab the variables will keep exist as local variables. Which are cleared in the same way as usual.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/30190950/global-variable-in-matlab-that-retains-an-old-value-and-wont-change

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils