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 ?

回答(2 个)

Subhajyoti
Subhajyoti 2024-10-23,14:42
Hi @Mike,
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);
State: 16, new-val: 16
newVal = randi(100);
ctx = StateManager(newVal);
fprintf('State: %d, new-val: %d\n', ctx, newVal);
State: 109, new-val: 93
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:

Mike
Mike 2024-10-24,9:46
Thanks for that - that provides some reassurance.
The key issues for me are:
calling a matlab app as a library resource I want to understand whether the app internal state is persistent.
I can see that for individual functions, use of static wil lpreserve the local function state.
In this app , the gui objectis used to host a collection of handles to state resources, including data objects.
I anticipate calling the app.initate function to create the internal state and then subecquent calls to manipulate app data directly through new library calls. those library calls will need to be able to access the internal app state saved on the gui handle object.
Is this meanigful in a compiled library which essentially wraps the app ?
Ie could I call my new matlab function doSomething() which internally calls readMapData( hObject.appName, data1[] , data 2[] ) which uses the global hObject to determine a data context to read data back from ?
Time to suck it and see I guess..
Also - whats the best way to trace the calls into Matlab for debugging purposes ?
cheers
Mike

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by