A method for such warning would cause a tremendous trouble. See this script:
a = 0;
for k = 1:10
a = a + 1;
end
Now each iteration would display a warning, because a is overwritten. What about the automatically created variable "ans", when an output is not caught?
Using sloppy scripts on one hand, but expecting a secure access of the calling workspace is a contradiction. If you care about reliable code and a restricted access to the base workspace, us functions. If you really need to create a set of variables in the base workspace finally (is this really required for calling Simulink?), use function to create a struct at first, and copy the fields of the struct to the workspace at the end:
function Main
S = struct([]);
S = yourFunc1(S);
S = yourFunc2(S);
PokeStructToBase(S);
end
function S = yourFunc1(S)
S.Param = 7.187;
end
function S = yourFunc2(S)
S.Value = pi + 2;
end
function PokeStructToBase(S)
F = fieldnames(S);
for k = 1:numel(F)
assignin('Base', F{k}, S.(F{k})));
end
end
6 Comments
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/440680-warning-while-overwriting-a-workspace-variable#comment_662473
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/440680-warning-while-overwriting-a-workspace-variable#comment_662473
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/440680-warning-while-overwriting-a-workspace-variable#comment_662490
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/440680-warning-while-overwriting-a-workspace-variable#comment_662490
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/440680-warning-while-overwriting-a-workspace-variable#comment_662496
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/440680-warning-while-overwriting-a-workspace-variable#comment_662496
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/440680-warning-while-overwriting-a-workspace-variable#comment_662499
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/440680-warning-while-overwriting-a-workspace-variable#comment_662499
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/440680-warning-while-overwriting-a-workspace-variable#comment_662506
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/440680-warning-while-overwriting-a-workspace-variable#comment_662506
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/440680-warning-while-overwriting-a-workspace-variable#comment_662516
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/440680-warning-while-overwriting-a-workspace-variable#comment_662516
Sign in to comment.