Matlab compiler seems to change the path to a "Onedrive" folder.

조회 수: 12 (최근 30일)
Mats Lindqvist
Mats Lindqvist 2022년 12월 19일
답변: Divyam 2025년 1월 3일
I have a matlab function that I want to share with some colleagues that don't have matlab, by creating a standalone app with Matlab Compiler.
In the function, I want to read input data from an excel sheet, to which I have entered an absolute search path, like this:
[x,txt,raw]=xlsread('C:\Folder\excelSheet.xlsm','Sheet1','A1:A3')
Then I package the function using Compiler, move those files to a different computer, install the application. I also create that exact same folder & excel sheet in that folder: "C:\Folder\excelSheet.xlsm".
When I then run the executable using a VBA script, (Call Shell("C:\executableFolder\MatlabFunction.exe") ) I get an error message saying it can't find that Excel file: "Unable to open file 'excelSheet.xlsm'. File 'C:\Users\MyName\OneDrive-MyCompany\Documents\excelSheet.xlsm' not found.
I.e. it doesnt look for the excel sheet in the folder location that I specified, but in a different "OneDrive" folder.
I then try to run that executable from a VBA macro, in the very same excel sheet, from which the intput data is to be retrieved. The plan is to enter some inputs in a few excel sheet cells, click a button, so that the matlab file reads its input data, runs the function, and then read the results back into the excel sheet.
What am I doing wrong?
I should add that if I just run the exe file (residing in its installation folder) just by double-clicking it, it runs fine, it reads the input data (but from the Excel file that resides in the same folder as the exe file, not from that absolute path. The same error happens if I create the vba script in this excel sheet, and run the exe from excel.
/M

답변 (1개)

Divyam
Divyam 2025년 1월 3일
The MATLAB Compiler Runtime might resolve file paths differently when invoked from contexts such as VBA scripts. It may happen that it sets the current working directory to where the executable resides and then tries to find the excel file there.
This issue can be mitigated by dynamically resolving the paths to the excel file based on the location of the executable.
% Fetch the path of the executable
currentFolder = fileparts(mfilename('fullpath'));
% Save the excel file in the same folder as the executable and construct
% the full path to the executable
excelFilePath = fullfile(currentFolder, 'excelSheet.xlsm');
% Read from the Excel file
[x, txt, raw] = xlsread(excelFilePath, 'Sheet1', 'A1:A3');
For more information regarding the "mfilename" function, refer to this documentation: https://www.mathworks.com/help/matlab/ref/mfilename.html
For more information regarding the "fileparts" function, refer to this documentation: https://www.mathworks.com/help/matlab/ref/fileparts.html

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by