Matlab: How to set variable if it doesnt exist yet

The following code snippet shows how to set a variable in Matlab only if it doesn’t exist yet:

set_variable_if_not_exists.m
if ~exist('my_variable', 'var')
    my_variable = 0; % or whatever default value you want to set
end

Check out similar posts by category: Matlab/Simulink