How to access App version number from inside the App

29 ビュー (過去 30 日間)
Steven
Steven 2023 年 11 月 22 日
コメント済み: Steven 2023 年 11 月 27 日
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?

採用された回答

Jonas
Jonas 2023 年 11 月 23 日
編集済み: Jonas 2023 年 11 月 23 日
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
  1 件のコメント
Steven
Steven 2023 年 11 月 27 日
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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Compiler についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by