Matlab python package accepting external files

1 view (last 30 days)
Hello,
I created compiled Matlab python package to be used in a python environment, hopefully seamlessly. The program runs fine, but for it to work it has to fetch some information from a .txt file that is converted to a .mat file in the Matlab script. The Matlab script prompts the user for the name of the file, since there can be many different variations. These .txt files are otherwise already in the same folder as the script itself, but when I specified the function that is to be exported, all the other required function for the program to work were exported with it, but the .txt files weren't. My question is, which location do I need to place my .txt files for them to be discoverable by the Matlab runntime like they are in the Matlab IDE environment.
I apologize if this is a stupid question, I am fairly new with Matlab and completely new with packages!
Thank you!

Answers (1)

Kojiro Saito
Kojiro Saito on 29 Aug 2018
Before compiling, MATLAB Compiler calculate file dependencies and .txt files are to be included automatically in "Files required for your library to run".
If not, you can manually include .txt files by clicking "+" button.
If your .txt files are changeable, it's better not to include .txt files in the compiled application and we can pass the file path to compiled MATLAB functions as an input argument.
fileReadDeployTest.m
function out = fileReadDeployTest(fname)
fileId = fopen(fname,'r');
out = fread(fileId, '*char');
fclose(fileId);
end
test.txt
test
After python setup.py install is done, you can call the deployed function from Python. In this case, test.txt exists in the same folder of test.py. You can pass the .txt locations as an argument to MATLAB function. Both the relative path (just 'test.txt') and absolute path will work.
test.py (tested in Python 3.5 of Windows)
import fileReadDeployTest
myApp = fileReadDeployTest.initialize()
out = myApp.fileReadDeployTest('test.txt')
# Absolute path is also OK
#out = myApp.fileReadDeployTest('C:\\Work\\test.txt')
print(out)
myApp.terminate()

Categories

Find more on Python Package Integration in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!