Is using global variables for additional parameters in a callback function safe for deployment?
8 次查看(过去 30 天)
显示 更早的评论
MathWorks Support Team
2022-2-1
编辑: MathWorks Support Team
2024-8-29
I am using a MATLAB function that requires me to use a callback function. However, my callback function takes some extra parameters that are not the default for this type of callback. To pass in my extra data, I've used global variables.
For example, here's some code that updates my plot using a 'lineCallback' with some additional parameters.
global lineStyle marker
lineStyle = '--';
marker = '*';
plot(x, y, 'ButtonDownFcn', @lineCallback);
And my callback function
function lineCallback(src, ~)
global lineStyle marker
src.Color = 'red';
src.LineStyle = lineStyle;
src.Marker = marker;
end
Now I would like to compile this code and deploy it using MATLAB Compiler. Is this safe to do?
I heard that usage of 'global' variables can be unsafe. Please run the below command in the command window of installed MATLAB R2019a version to get release specific documentation that describes this information:
>> web(fullfile(docroot, 'matlab/matlab_prog/share-data-between-workspaces.html'))
Please follow the below link to search for the required information regarding the current release:
采纳的回答
MathWorks Support Team
2024-7-28
编辑:MathWorks Support Team
2024-8-29
As mentioned by the above documentation page, this is not safe to do. This is for a few reasons, but two of the most important are detailed below.
The first reason is that your usage of global variables may not be safe within MATLAB, and things are going wrong, but this goes unnoticed. This is because global variables do not necessarily throw an error if they are misused (e.g. overwritten by setting another global variable), as mentioned in the page you linked.
The second, more important reason is that often, deployed code is used in an environment that has more going on than your MATLAB environment. For example, for a multi-threaded program, one thread might set the global variables to one value, and the second thread may come in and change the values. In this case, there would be no error messages and the answers would be wrong, so these may errors would likely be difficult to detect. These kinds of problems would be very difficult to debug.
We strongly recommend removing the global variables and instead passing additional arguments. Please run the below command in the command window of installed MATLAB R2019a version to get release specific documentation which describes the syntax to pass additional arguments:
>> web(fullfile(docroot, 'matlab/creating_plots/callback-definition.html'))
That is, the function call could look like this:
plot(x, y, 'ButtonDownFcn', {@lineCallback, '--', '*'});
And the function:
function lineCallback(src, event, lineStyle, marker)
src.Color = 'red';
src.LineStyle = lineStyle;
src.Marker = marker;
end
Please follow the below link to search for the required information regarding the current release:
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Compiler SDK 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!