Is using global variables for additional parameters in a callback function safe for deployment?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MathWorks Support Team
el 1 de Feb. de 2022
Editada: MathWorks Support Team
el 29 de Ag. de 2024
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:
Respuesta aceptada
MathWorks Support Team
el 28 de Jul. de 2024
Editada: MathWorks Support Team
el 29 de Ag. de 2024
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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre MATLAB Compiler SDK en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!