How to access App version number from inside the App

23 views (last 30 days)
When compiling an App I give it a version number (eg. 2.1.1). This show up by right-clicking the App.exe file that is installed. However, I need to write this version number into log files created by the App. (so we know which version was used - it's a tracability requirement)
At the moment the only way I can see to do this is to manually write this version number in two places - once in the Application Infomation of the compiler project and once someplace in the MATLAB scripts. This is subject to human error (forgetfulness and typos)
Is there a way to have this in one place, so there is a single point of truth?
Failing that, is it possible to write a Unit Test that runs automatically, after every compile, that check the two version number match?

Accepted Answer

Jonas
Jonas on 23 Nov 2023
Edited: Jonas on 23 Nov 2023
hi Steven,
do you have powershell available (are you on windows)?
then have a try with the attached m file and the prj file for the application compiler to make sure you can see the command line
the content of the m file is just that, at the moment the application name is hard coded, but usually this will not change?
content of showVersionNumber.m
disp('from cmd:');
[~,cmdout]=system('powershell -command "(Get-Command .\showVersionNumber.exe).Version"')
disp('extract version number')
strjoin(regexp(cmdout, '\d+', 'match'),'.')
output looks like that:
if your application name matches the filename, you could work also with
name = coder. mfunctionname;
and code it in that way into the string given in the powershell command
  2 Comments
Steven
Steven on 27 Nov 2023
Thank you, that does just what I need.
I made it a function I could call from my App's startupFcn() and added a couple error checks, such as no powershell or running in the MATLAB environment rather then as a compiled app.
function str = getAppVersion()
if isdeployed
% Get the application version from the .exe
[error,cmdout]=system('powershell -command "(Get-Command .\MyApp.exe).Version"');
if error
str = "--"; % system command failed (no powershell maybe)
else
str = "v" + strjoin(regexp(cmdout, '\d+', 'match'),'.');
end
else
str = "Not deployed"; % there is no .exe (yet)
end
end
dpb
dpb on 1 May 2025
Edited: dpb on 3 May 2025
I also recast the original some to be more generic but with similar purpose in mind...but this one returns the version as a struct so can extract the pieces desired rather than just the long string...
function [status,version,cmdout]=getexeversion(app)
% return executable file version info
cmdout=[];
if ~isfile(app), status=-1; version=[]; return, end % no such file found
cmdstr=['powershell -command (Get-Command """',app,'""").Version'];
[status,cmdout]=system(cmdstr);
if status, version=[]; return, end % command failed
%elements=regexp(cmdout, '\d+', 'match'); % original stripped numbers returned powershell line,column if failed
%version=strjoin(regexp(cmdout, '\d+', 'match'),'.'); % long string returned as Major.Minor.Build.Revision
version=cell2struct(extract(cmdout,digitsPattern),extract(cmdout,lettersPattern)); % return as fields of struct instead
end
powershell is such an arcane beast I couldn't figure out how to get it to return its error code; with the limitation of launching from MATLAB which, unfortunately, only know about the CMD shell, all one gets back is whether or not CMD succeeded or not...and even if there is no file that matches such that 'Get-Command....Version' fails, CMD still succeeds so that's of no help. In that case, the original still returned a version string ('1.2') because the output error returned included the line and position in which the error was found and the regexp found those...
cmdout =
'Get-Command : The term 'fred.exe' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:2
...
Hence the check first that the asked-for file does actually exist. I suppose it should also check that it is a valid file type as well...
As an aside, it is surely annoying that one has to go through such machinations; there appears to be no way to return the version sharing data that is in the app under the toplevel app object.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB Compiler in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!