Is using global variables for additional parameters in a callback function safe for deployment?

8 ビュー (過去 30 日間)
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
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 件)

カテゴリ

Help Center および File ExchangeMATLAB Compiler SDK についてさらに検索

タグ

タグが未入力です。

製品


リリース

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by