- The variables are declared global (using global is a bad practice, you should not declare global variables);
- You are using the debugger and stopped during the execution of the function (in that case what you see is the function workspace);
- The variables are outputted by the function into a script as in the following:
How do I stop matlab automatically converting a normal variable into a global variable that I defined previously but deleted.
4 views (last 30 days)
Show older comments
Anuj Anand
on 17 Jan 2014
Answered: Anuj Anand
on 17 Jan 2014
I am having a strange problem. I had defines a variable as global variable with name 'mog'. Steps are following,(1)I ran the code/function with global variable definitions.(2)I then deleted the lines in my code where the global variable was defined.(3)I then cleared the global variable using 'clear global' and/or 'clearvars -global mog' as well (According to me, i do not need to do this probably because if i deleted the definition of the global variable it should not exist in my work space)(4)Then I closed and restated matlab. (5)Then I started writing a new function and defined a normal variable with name 'mog', but matlab for some reason makes it a global variable automatically... And I do not understand why? And how can I stop this variable 'mog' to be automatically converted into a global variable.
0 Comments
Accepted Answer
Bruno Pop-Stefanov
on 17 Jan 2014
Variables declared in a script exist in the base workspace. However, variables declared inside a function only exist in the function workspace. In other words, they are destroyed when the function returns and you shouldn't see them in the base workspace unless:
% Function file foo.m
function c = foo(a,b)
c = a + b;
end
% Script file script.m
clear all
close all
clc
c = foo(10, 12); % c is in the base workspace, because it was declared in a script
Could you copy and paste your function where you declare mog? If you want to learn more about functions and the scope of a variable, you can refer to the online documentation on functions .
0 Comments
More Answers (1)
See Also
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!