How can I declare a global variable in Matlab for a few functions?
global, matlab, variables
Solution
You can gain access to a global variable inside a MATLAB function by using the keyword `global`:
function my_super_function(my_super_input)
global globalvar;
% ... use globalvar
end
You will usually declare the global variable in a script outside the function using the same keyword:
% My super script
global globalvar;
globalvar = 'I am awesome because I am global';
my_super_function(a_nonglobal_input);
However, this is not strictly necessary. As long as the name of the global variable is consistent between functions, you can share the same variable by simply defining `global globalvar;` in any function you write.
All you should need to do is define `global PIXELS;` at the beginning of each of your functions (before you assign a value to it).
See the official documentation here.
Problem
I have a file called `histShape.m` with a function `histShape` , and some other functions also . A general view of the code is : ``` % function [outputImage] = histShape(srcimg, destimg) PIXELS = 255 + 1; .... .... end % function [outputImage] = normalizeAndAccumulate(inputImage) PIXELS = 255 + 1; .... .... end % function [pixels] = getNormalizedHistogram(histogram , inputImage) PIXELS = 255 + 1; .... .... end ``` I can use `global x y z;` but I'm looking for a different way . I want to declare the variable `PIXELS` as global , how can I do that ? Regards