running matlab exe file results in "Too many input arguments. MATLAB:TooManyInputs error"

5 次查看(过去 30 天)
When compiling a MATLAB (.m) function (main_file below is code) into a standalone executable (.exe) through MATLAB Compiler, the command-line arguments passed to the executable are not being accepted. Instead of parsing the arguments through the getmcruserdata('syscommandargs') function within the compiled MATLAB application, MATLAB is throwing an error, stating there are "Too many input arguments. the following code works fine when i dont pass any args (main_file.exe). however with passign args I get error. Note that I expect when i pass args the disp('Not enough arguments received.'); print message but it does not. I made the following function to exe which is named main_file.exe. Running main_file.exe "test1args" "test2args" results in error (Too many input arguments. MATLAB:TooManyInputs). Please help me to resolve it. Thanks.
function main_file
param1 = 'test1args';
param2 = 'test2args';
if isdeployed
disp("Reading input arguments");
args = getmcruserdata('syscommandargs');
if isempty(args)
disp('Not enough arguments received.');
else
% Parse args
args_cell = strsplit(args{1}, ' ');
if numel(args_cell) >= 2
param1 = args_cell{1};
disp(param1);
param2 = args_cell{2};
disp(param2);
else
disp('more than 2 arguments received.');
end
end
else
disp('Running in MATLAB');
param1 = 'test1args';
disp('test2args');
disp(param1);
param2 = 'Yes';
disp('param2');
disp(param2);
end
disp("hello from executable...")
app = RunLocallyMat();
disp("init done")
app.main_process(param1, 'process', param2);
disp("process done")
end

采纳的回答

Walter Roberson
Walter Roberson 2024-4-23
You need
function main_file(varargin)
When you compile an executable, command line arguments are passed as parameters to the main function . You defined function main_file without parameters, so running with no parameters at least gets the code started, but running with parmeters immediately fails for having passed extra parameters to main_file
  1 个评论
Amin
Amin 2024-4-24
编辑:Amin 2024-4-24
thanks. the following resolve the issue.
function main_file(varargin)
p = inputParser;
addParameter(p,'case1','case1_val',@ischar);
addParameter(p,'case2','case2_val',@ischar);
parse(p, varargin{:});
param1 = p.Results.case1;
param2 = p.Results.case2;
if isdeployed
disp('Running using exe');
disp("Reading input arguments");
disp(p.Results)
else
disp('Running in MATLAB');
disp(['case1: ', param1]);
disp(['case2: ', param2]);
end
disp("hello from executable...")
app = RunLocallyMat();
disp("init done")
app.main_process(param1, 'process', param2);
disp("process done")
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by