필터 지우기
필터 지우기

How can I copy a resource file in a packaged / compiled app?

조회 수: 9 (최근 30일)
McLain Cowan
McLain Cowan 2021년 7월 30일
답변: Vidhi Agarwal 2024년 5월 10일
I have written an app using AppDesigner and I can't seem to figure out how to save my file using a template I created and placed in the resource folder in my packaged app.
The Error I continue to get is 'No Matching files were found.'
filename_template = 'DesignData_template.xlsx';
[file,path] = uiputfile(sprintf('DesignData, Panel %.0f.xlsx',i));
filename = fullfile(path,file);
copyfile(filename_template, filename, 'f');

답변 (1개)

Vidhi Agarwal
Vidhi Agarwal 2024년 5월 10일
Hi McLain Cowan,
While working with App Designer and facing an issue with accessing a file (like your DesignData_template.xlsx template) after packaging the app, often relates to how the app's file paths are resolved at runtime. Packaged apps have a different current working directory than you might expect, which can lead to "No Matching files were found" errors if the app tries to access files using relative paths that are no longer correct post-packaging.
Here’s a step-by-step approach that can be helpful in resolving such kind of issue.
  • When packaging a MATLAB app, include resources like your Excel template in the app's archive. To access these resources at runtime, dynamically find their path, as installation paths can vary across different users and systems.
  • For packaged apps, use the “ctfroot function to find the runtime extraction folder for accessing resources. ctfroot" is applicable only to compiled apps. For non-compiled apps run from MATLAB, use mfilename('fullpath') to find your app's main file and derive the resource path from there.
  • Create a utility function in your app to find if it's running as a compiled app or within MATLAB and construct the resource path accordingly. Here's an implementation example:
function resourcePath = getResourcePath(resourceName)
% resourceName: Name of the resource file, e.g., 'DesignData_template.xlsx'
if isdeployed
% If running in compiled app, use ctfroot
baseDir = ctfroot;
else
% If running in MATLAB, use the path to this file
baseDir = fileparts(mfilename('fullpath'));
end
% Construct the path to the resources folder
resourcePath = fullfile(baseDir, 'resources', resourceName);
end
  • Now, you can use this utility function in your app to get the correct path to your template file, ensuring it works both when running from MATLAB and after packaging:
filename_template = getResourcePath('DesignData_template.xlsx');
[file, path] = uiputfile(sprintf('DesignData, Panel %.0f.xlsx', i));
if isequal(file, 0) || isequal(path, 0)
% User canceled the operation
return;
end
filename = fullfile(path, file);
copyfile(filename_template, filename, 'f');
  • When using the Application Compiler to package your app, include the resources folder (having your Excel template) to ensure “DesignData_template.xlsx” is available in the packaged app.
For better understanding of “ctfroot”, refer to the following documentation:

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by