Exchange data between MATLAB-compiled standalone application and MATLAB

10 vues (au cours des 30 derniers jours)
Marko Gulin
Marko Gulin le 23 Déc 2019
Modifié(e) : Mohammad Sami le 24 Déc 2019
How to exchange data between MATLAB-compiled standalone application and MATLAB?
Here is the minimum working example.
1- Class file that creates a simple GUI with label that outputs some message.
classdef example < handle
properties (Access = private)
fig
console
end
methods (Access = public)
function obj = example(msg)
obj.fig = uifigure( ...
'Name', 'MWE', ...
'Position', [200 200 300 50] ...
);
obj.console = uilabel( ...
'Parent', obj.fig, ...
'Position', [10 10 280 30], ...
'Text', msg ...
);
end
function updateConsole(obj, msg)
obj.console.Text = msg;
end
end
end
2- The following code creates an instance of class 'example' and immediatelly outputs "Initial message.". After 5 seconds, it updates the output to "Second message."
% Create GUI and output message
obj = example("Initial message.");
% Wait 5 seconds
pause(5);
% Update message in GUI
obj.updateConsole("Second message.");
3- Now let us compile the program to standalone .exe application and place output files in build/ subdirectory as follows:
mcc -m example.m -d build/
4- The compiled standalone .exe application is called from MATLAB's Command Window as follows:
!build\example "Initial message."
The former code opens the GUI and I can immediatelly see "Initial message.". However, can I update the output message somehow? After reading some Q/A, I understand that .exe application does not expose methods from the class file to the outside world. I was thinking to use some external file to exchange data, I could use memmapfile for this purpose.
Another option would be to compile the application as a package, in which way I would be able to use internal functions. The only problem with this approach is that MATLAB Compile SDK does not allow me to compile class files, only "functions and mex files with single entry point". I know I could create a wrapper function around that class, however, is it possible to create a package that is supposed to be used by MATLAB afterwards?
If suggestion is to use "external file" to communicate with the application, I'm not sure how to deal with the "race" problem, i.e., I would like to lock the file while one process is accessing it. I could have a lock flag in the file, e.g., when flag is TRUE the file is locked, otherwise the file is unlocked. However, I'm afraid that two or more processes could try to lock the file at the same time. Should I consider this potential issue, and if yes, how can I deal with it? A potential solution to this issue is to write a process ID to the lock flag, wait some time (e.g., 10 ms), read back the lock flag (i.e., process ID) from the file, and proceed with modifying the file only in the process with matching ID. In this case, even if some process overwrites the lock flag with its own process ID, the "read back process ID" function will prevent a potential clash. What do you think?
Thank you!

Réponses (1)

Mohammad Sami
Mohammad Sami le 24 Déc 2019
Modifié(e) : Mohammad Sami le 24 Déc 2019
Hi,
I would suggest that in your code, you load the messages as either a .mat file or a .txt file.
messages = load('messages.mat');
% assume there are two variables in messages.mat, msg1, msg2
% Create GUI and output message
% alternatively use any of the reading function to load any other kinds of files.
obj = example(messages.msg1);
% Wait 5 seconds
pause(5);
% Update message in GUI
obj.updateConsole(messages.msg2);
The compiled code will look for the file 'messages.mat' in the working directory of the exe file.
During compiling you can simply add it under the files "Files installed for your end user".
Or you can simply put it in the same directory as the exe file.
To pass data between multiple instances, you may perhaps want to run an external database and read and write to the database. The database program will be able to handle the synchronicity issues.
Alternatively you can look into the various java functionality.

Catégories

En savoir plus sur MATLAB Compiler dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by