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

5 views (last 30 days)
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');

Answers (1)

Vidhi Agarwal
Vidhi Agarwal on 10 May 2024
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:

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!