- https://www.mathworks.com/help/matlab/ref/persistent.html
- https://www.mathworks.com/matlabcentral/answers/101556-is-it-possible-to-define-a-variable-as-static-within-a-matlab-matlab-file
Does Matlab support persistence of data in a compiled python library
22 次查看(过去 30 天)
显示 更早的评论
I want to package a set of Matlab functions ( quite a large set of code provided to me as a GUI app) to provide a python interface to a python REST server. I no longer need the GUI.
I want to know whether the python compiled library supports the internal use of handles or other means as persistent context stores so I can call one function to update the internal state and then call another, which uses the updated state to do something else without haveing to pass the state in and out across the interface.
The code uses a lot of data in excel format. Will the code library also support the embedd use of Excel or do I need to reformat my excel data to , say, Json or .mat files ?
0 个评论
回答(2 个)
Subhajyoti
2024-10-23,14:42
It is my understanding that you are trying implement static contexts instead of always passing them across the interface. And perform embedded use of Excel from the compiled Python Library.
You can define a variable as persistent to give "static-like" behaviour in a MATLAB function, but its scope is limited within that function.
Here, in the following implementation (say, ‘StateManager.m’), I have used persistent variable, and appended its value to an excel file using “tablewrite”.
function result = StateManager(val)
persistent ctx;
if isempty(ctx)
ctx = 0;
end
ctx = ctx + val;
result = ctx;
% append to excel file
filename = 'state.xlsx';
if ~exist(filename, 'file')
writetable(table(ctx), filename);
else
writetable(table(ctx), filename, WriteMode='append');
end
end
When the function is called repeatatively, the previous values are retained in memory between calls.
newVal = randi(100);
ctx = StateManager(newVal);
fprintf('State: %d, new-val: %d\n', ctx, newVal);
newVal = randi(100);
ctx = StateManager(newVal);
fprintf('State: %d, new-val: %d\n', ctx, newVal);
Refer to the following MathWorks documentation links to learn more about ‘persistent’ variables in MATLAB:
Additionally, you can refer to the following resources to know more about using python packages of MATLAB Functions:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!