In Matlab when declaring a variable as global and save it using the save() command, the variable is also global after loading the .mat-file in a new session. Following code shows this behavior:
In the beginning, I have no variables:
>> who
>> who global
Then, I create the global variable and save it:
>> global settings
>> settings.test = 1;
>> who
Your variables are:
settings
>> who global
Your variables are:
settings
>> save('test.mat','settings');
After that I clear the workspace and global variables (or start a new Matlab session)
>> clear
>> clearvars -global
>> who
>> who global
When I then load the .mat file, the variable is again marked as global, even when I don't specify it now.
>> load test.mat
>> who
Your variables are:
settings
>> who global
Your variables are:
settings
>> clear
>> who
>> who global
Your variables are:
settings
Is there any way to prevent this behavior?
It seems to me, the "global" flag is saved with the variable. Is this really useful? Suppose one sends me a mat-file with data, where variables are declared as global. Even when loading this file in function, it will spread the data in my complete session. For me this makes Matlab code very vulnerable.
Thank you in advance.
settings = load test.mat? Is that stillglobal? - Dansaveandloaddocs) - Dan