Clear Filters
Clear Filters

How to wrap up an .exe with input argumentsand launch it from c# ?

21 views (last 30 days)
Hi everyone! I am in the middle of a programme involving both c# and matlab (2019b). I can wrap up the .mlapp file in App Designer into .exe and call it from c# like:
// c# code
var txtMWArray = new MWCharArray(@"E:\db_bogie\hh.txt"); // the argument to be passed to .exe
var matExe = Process.Start(matPath, "txtMWArray"); // start up the .exe
But when I call the .exe (i.e. what 'matPath' leads c# to) with the second line in this code, matlab cannot recognise MWCharArray, or c# fail to convert 'txtMWArray' into chars in matlab. C# throws the error message starting with "Unrecognised function or variable 'event' ".
Something I have tried:
1. Start an .exe without any arguments would be fine, and merely passing those arguments (no converting or processing, just "a = inputArg" in matlab) would invoke no error;
2. I tied to use class(a) to read the type of the input argument, and it turned put to be System.object, a c# thing;
3. In matlab, I fail to convert MWCharArray to a string with func string().
4. The stratup func in matlab be like:
function startupFcn(app, savePath, readPath)
% savePath = the 1st arg, supposed to be a string
% readPath = the 2nd arg, supposed to be a 1xn cell array
end
So in summary, I want to start an .exe compiled by Matlab App Designer and pass arguments to it. I do not kown if I have done someting wrong in compiling .exe or launching it from c#.
Thank u and wish u a nice day!

Answers (1)

Pratyush
Pratyush on 28 May 2024
Hi Tyreke,
The way you're attempting to pass MWCharArray from C# to MATLAB is not directly applicable for a standalone executable compiled from MATLAB.
To successfully pass arguments from a C# application to a MATLAB compiled executable, follow these steps:
  1. Ensure your MATLAB function, especially the 'startupFcn' for App Designer apps, is designed to accept and parse string arguments. Compiled MATLAB applications receive command-line arguments as strings.
  2. When compiling your MATLAB app into an executable, make sure it's configured to accept command-line arguments, which will be treated as strings by the executable.
  3. Modify your C# code to correctly format and pass arguments as strings to the MATLAB executable. Use 'Process.Start' with the path to the executable and the arguments formatted as a single string, ensuring to include quotes if the arguments might contain spaces.
  4. The error you encountered ("Unrecognised function or variable 'event'") suggests there might be an issue within your MATLAB app's handling of the startup arguments. Review the 'startupFcn' to ensure it's prepared to handle and parse string inputs without expecting MATLAB-specific data types.

Categories

Find more on Programming Utilities in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!