App Designer standalone app can't find external file

I have an App Designer app that includes a text file for calculations. When I debug, it works correctly.
But when I create a 'Standalone Desktop App and install the program, the app can't find the included text file and crashes. I see the included file in the same directory as the .exe file. Here is my code:
% Load the CTP Timing Protocol File for Processing
ctp_tpfilename = 'HB_CTP.txt';
f = fopen(ctp_tpfilename);
data = textscan(f,'%s');
fclose(f);
registers = data{1}(2:2:end);
registers = convertCharsToStrings(registers);
...
Am I missing the path to the file? I dont know the path it is trying, since this is the compiled application. How can I tell it to look in the same directory are the main executable file?
Thanks in advance.

5 Comments

Use fprintf(pwd) and see the output in the console to identify where the .exe is, so you can check if the file is in that folder.
I do not have a console in the compiled gui app. Is there a way for the path to be shown in a popup message box maybe?
On Windows you'll have a problem with fprintf(pwd) because of the backslashes:
wd = 'C:\Users'; % Windows
fprintf(wd)
Warning: Escaped character '\U' is not valid. See 'doc sprintf' for supported special characters.
C:
wd = pwd; % Linux/Mac
fprintf(wd)
/users/mss.system.vsklTz
However, fprintf('%s\n',pwd) will work in all cases:
wd = 'C:\Users'; % Windows
fprintf('%s\n',wd)
C:\Users
wd = pwd; % Linux/Mac
fprintf('%s\n',wd)
/users/mss.system.vsklTz
@Voss But how do I see the current directory path in a compiled standalone windows desktop application? Once I found that out, I can hard code the path to my .txt file in the code inorder to read it in.
When you compile using deploytool, uncheck the box under "Additional runtime settings" that says "Do not display the Windows Command Shell (console) for execution". This will pop up a console window when the exe runs and any command-line output will go to that console, including the result of fprintf('%s\n',pwd).

Sign in to comment.

 Accepted Answer

Thank you for all the suggestions. I figured it out. I had to add this bit of code to determine if the app was deployed as a standalone app or run in Matlab.
if isdeployed
[status, result] = system('path');
installpath = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'));
else
installpath = pwd;
end
Then I added this to my read lines:
f = fopen(fullfile(installpath, ctp_tpfilename));
data = textscan(f,'%s');
fclose(f);
Now it reads the included file in the installed directory or my current directory while debugging.

More Answers (2)

chrisw23
chrisw23 on 27 Oct 2023
Moved: Rik on 27 Oct 2023
You have to add dependent files manually in the Compiler App Project before building the standalone component. Since there's no path specified, the file is expected beside your app executable. The app will not be executed in the path where it will be started. It's executed (extracted) in a users temp folder. That's where your file is expected to be loaded without a path specified.
...your app path is somewhere here
C:\Users\<userName>\AppData\Local\Temp\<userName>\mcrCache<MatlabRuntimeVersion>\<yourAppName>

5 Comments

Thanks @chrisw23.
I found that if I place the HB_CTP.txt file in the above location you mentioned (C:\Users\<userName>\AppData\Local\Temp\<userName>\mcrCache<MatlabRuntimeVersion>\<yourAppName>), my app finds it and excutes correctly.
Now, how can I compile the standalone app so the app knows to look into the same folder as the exe file?
@Scotty Mac: When you compile your .txt file into your executable (by adding the .txt file in the "Files required for your application to run" section of deploytool), as @chrisw23 suggests, then everything in the exe (including the txt file) is extracted to some temporary folder, which means you can refer to it by file-name only in your code, like the code in your original question does, and it should work because the txt file is in the folder where the exe is running (i.e., the temporary folder).
@Voss But if do that, I wont be able to change the .txt file outside of the app, correct? I need to able to edit that included .txt file with different data values, without having to recompile the GUI app.
That's right, so compiling the txt with the exe is not the way to go.
Typically, I would put some UI components in the app that allow the user to select the file they want to use, i.e., with uigetfile(), and store the full path to the file as an app property or whatever.
I thought so, thanks.
In this case, I dont want the user to able to select the data file (I have that functionality for loading & saving in another place of my GUI app. This external file is strictly a data file for my algorithm to use. It should transparent to the user. If I want to change a value or two, I'll know where that file is and can change it on the fly, without recompiling the app.

Sign in to comment.

Scotty Mac
Scotty Mac on 27 Oct 2023
Edited: Scotty Mac on 27 Oct 2023
I have included the HB_CTP.txt in the compiler app when creating the Standalone Desktop App. Once installed, I see the required .txt file in the same directory as the .exe file (see below both highlighted):
But it seems like the app can't find that included file...

2 Comments

How are you running the exe? E.g., just double-clicking on it? Or using a desktop shortcut? Or running it programmatically, e.g., from a Windows/DOS console?
Referring to 'HB_CTP.txt' in the code makes the code look for that file in the working directory, i.e., the directory returned by pwd().
The working directory when the exe starts is the location where the exe is run from. If double-clicking, the working directory is the location of the exe, in which case the code should find the text file, since it is in the same directory. If running the exe some other way, then the working directory may not be the location of the exe, and you can expect the program not to find the text file.
What happens when you run the exe? Do you get an error message about using textscan() with an invalid file identifier?
I am running it from the installed shortcut. I will put debugs in to see what path it is using.

Sign in to comment.

Categories

Products

Release

R2019a

Asked:

on 26 Oct 2023

Answered:

on 30 Oct 2023

Community Treasure Hunt

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

Start Hunting!