A MATLAB app designer app, after compiled to exe, the exe cannot know its own file path.…
- User
A MATLAB app designer app, after compiled to exe, the exe cannot know its own file path. Knowing this, how does the exe read and display its own ProductVersion / version number?
Assistant
When a MATLAB App Designer application is compiled into an executable (.exe), accessing the executable's own file path and version information requires specialized approaches because the application cannot directly reference its executable path through standard MATLAB methods. Here's how a compiled MATLAB application can retrieve and display its own version information:
Modern Approach (MATLAB R2019b and Later)
For MATLAB releases R2019b or later, standalone application file version information is directly embedded in the executable file's metadata. However, the application still needs a mechanism to retrieve this information:
Using System Commands
Since the compiled application cannot directly access its own file path, you can use system commands to retrieve the executable's information:
function versionInfo = getAppVersion() if isdeployed % Get the executable's path using system commands [~, result] = system('echo %~dpnx0'); exePath = strtrim(result); % Use a temporary VBS script to get version info tempFile = [tempname '.vbs']; fid = fopen(tempFile, 'w'); fprintf(fid, 'Set objFSO = CreateObject("Scripting.FileSystemObject")\n'); fprintf(fid, 'Set objFile = objFSO.GetFile("%s")\n', strrep(exePath, '\', '\\')); fprintf(fid, 'Wscript.Echo objFile.Version\n'); fclose(fid); % Execute the script and get the version [~, version] = system(['cscript //nologo ' tempFile]); version = strtrim(version); % Clean up delete(tempFile); else % During development, return a placeholder version = '1.0.0.0 (Development)'; end versionInfo = version; endUsing Windows Registry Commands (Alternative)
Another approach is to use the Windows Registry command to find the executable information:
function exeInfo = getExeInfo() if isdeployed % Get process ID [~, result] = system('tasklist /FI "IMAGENAME eq myApp.exe" /FO CSV /NH'); parts = textscan(result, '%s', 'Delimiter', ','); exeName = strrep(parts{1}{1}, '"', ''); % Get executable path from registry [~, result] = system(['reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\' exeName '" /ve']); pathLine = regexp(result, 'REG_SZ\s+(.*)', 'tokens', 'once'); exePath = pathLine{1}; % Now you can get version info using similar VBS approach else % Development environment info end endOlder Approach (Before R2019b)
For MATLAB releases before R2019b, there is no direct way to retrieve the version number after installation[7]. The recommended workarounds are:
1. Embedding Version Display in the Application
Include code in your MATLAB script that displays the version number:
function showVersion() msgbox('Version number: 1.2.3'); end2. Writing Version Information to External Files
Include code in your MATLAB script to write a text file containing version information:
function writeVersionInfo() if isdeployed versionNumber = '1.2.3'; fid = fopen('version.txt', 'w'); fprintf(fid, 'Application Version: %s\n', versionNumber); fprintf(fid, 'Build Date: %s\n', date); fclose(fid); end endImplementation Considerations
-
Version Setting During Compilation: When compiling your application, you can specify the version number in the Application Compiler settings. This will embed the version information in the executable file's metadata[11].
-
Accessing Files from Deployed Functions: Use the
isdeployedfunction to check if the code is running in deployed mode and adjust the behavior accordingly[2]. -
Location of Generated Files: Be careful about where you write any files from your application. Do not save files under
ctfroot, as this location may be deleted or refreshed for each application run[2]. -
Path Handling: When your application needs to access other files, use
fullfileto ensure that file paths use the correct separators for the system[2]. -
File Access in Deployed Mode: Files within the CTF (Component Technology File) archives can be accessed using
ctfrootas a reference point, but the exact path structure depends on where the files were located during compilation[9].
For modern MATLAB versions (R2019b and later), the version information is embedded in the executable file itself, making it easier to retrieve and display within the application[7]. For older versions, the recommended approach is to embed the version display directly in the application code.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.