Standalone does not create a .mat file while in the app designer it does
Show older comments
Hey guys,
So I am this app for processing battery model parameters and I would to store each new battery to a .mat file. So in the code I pretty much make the directory and necessary folders for the files and then it creates an empty .mat file that will soon be filled with data using another function. See code below :
formatPathfolder = "./Battery cells - Database/%s";
pathfolderfile = compose(formatPathfolder, app.manufacturer);
addpath(char(pathfolderfile))
formatPath = "./Battery cells - Database/%s/%s";
pathfile = compose(formatPath, app.manufacturer, app.model);
addpath(char(pathfile))
formatMatfile = '%s_%s_S%dF%d_%d';
formatMatfileEval = '%s_%s_S%dF%d_%d = app.bcell(%d)';
for i = 1:app.nrcells
matfile = compose(formatMatfile,app.manufacturer, app.model, app.software_version, app.firmware_version, i);
matfileEval = compose(formatMatfileEval,app.manufacturer, app.model, app.software_version, app.firmware_version, i,i);
eval(string(matfileEval));
formatSave = "./Battery cells - Database/%s/%s/%s.mat";
savefile = compose(formatSave, app.manufacturer, app.model, char(matfile));
save(savefile,char(matfile));
end
formatMatfileMean = '%s_%s_S%dF%d_mean';
formatMatfileMeanEval = '%s_%s_S%dF%d_mean = app.bcell_mean';
meanfile = compose(formatMatfileMean,app.manufacturer, app.model, app.software_version, app.firmware_version);
meanfileEval = compose(formatMatfileMeanEval,app.manufacturer, app.model, app.software_version, app.firmware_version);
eval(string(meanfileEval));
formatSave = "./Battery cells - Database/%s/%s/%s.mat";
savefile = compose(formatSave, app.manufacturer, app.model, char(meanfile));
save(savefile,char(meanfile));
For some reason, this code works perfectly when I am executing the app in the app designer tool. But as soon as I make a standalone executable for the app and try to generate .mat files with it, it does not create it. Though I do see that it does create the necessary folders for it, but I'm not sure if this is a path finding issue or something else. Do you guys have any ideas as to what the issue might be?
Answers (1)
Jan
on 27 May 2021
It is a bad idea to add a folder to the current path only to access data files. Include only folders in Matlab's path, which contain M-files to be executes. In a compiled application this is not useful at all.
Use mkdir() to create the folders.
Use absolute path names instead. Relying on "." to be a specific folder is fragile.
Use fullfile() to create the path names. The approach with compose() is harder to read.
formatMatfileEval = '%s_%s_S%dF%d_%d = app.bcell(%d)';
This is a typical method to hide important information in the names of variables and file names. This apporach increases the level of complexity, if this information must be extracted later on.
You code would look clear, if you do not switch between CHAR vectors and strings such frequently. Decide for one format to represent strings.
2 Comments
Lynrick Wix
on 27 May 2021
Jan
on 27 May 2021
The "Matlab path" is a list of folders, Matlab searches functions in. If open a file, e.g. by fopen('FileName.mat') Matlab searchs in the current folder at first, then in all folders of the path. This can be misleading, because the user cannot know, which folders are included in the path. Therefore it is a good programming practice not to include folders in the path only to access files containing data.
% Assume you have a MAT file store in:
% C:\Temp\DataFolder\DataFile.mat
% The current folder is:
% C:\Temp\
% Bad:
Folder = cd;
DataFolder = compose('%s\DataFolder', Folder);
addpath(DataFolder);
Data = read('DataFile');
% Good:
Folder = 'C:\Temp\';
DataFolder = fullfile(Folder, 'DataFolder');
Data = read(fullfile(DataFolder, 'DataFile'));
This reduces the danger of side-effects by modifying the path or by relying on the current folder to be a specific folder.
Of course a user of your code can create MAT files and folders. Just do not use addpath, but mkdir and absolute path names. To get the folder of the current Matlab function:
if isdeployed % In compiled applications:
myFolder = ctfroot;
else % In M-functions:
myFolder = fileparts(mfilename('fullpath'));
end
Categories
Find more on MATLAB Compiler 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!