Is there a way to generate c code for system function which runs batch file?

I am trying to run a batch file which eventually runs an exe file. I am trying to use system function to run this batch file.But system is not supported for code generation. Is there a way to generate c code for system function or is there a way with which I can run a batch file which will be supported by code generation?

Answers (1)

In Unix systems, execv() or execvp() would often be used.

7 Comments

But I want to run a batch file using MATLAB function only.
You cannot do that when you generate C code. MATLAB only generates code that uses nothing more than the standard C library, and the standard C library does not have any routines corresponding to system() or unix() or dos() . As far as MATLAB Coder knows, the code is being sent to an FPGA or an embedded DSP that has no operating system, so you need to put in custom coder.ceval calls if you want something more.
https://www.mathworks.com/help/simulink/slref/coder.ceval.html I am just following this to generate c code. How do I link this my simulink model?
Use the "call external C function" example (the first one). You would want one input argument that is const char *
#ifdef MATLAB_MEX_FILE
#include <tmwtypes.h>
#else
#include "rtwtypes.h"
#endif
int execute();
This is my header file.
#include <stdio.h>
#include <stdlib.h>
#include "foo.h"
int execute()
{
return system("C:\\Users\\Tester\\Desktop\\Release\\meter_app.bat");
}
This is matlab function which calls execute function.File name is callexecute.m
function y = callexecute %#codegen
y = 0.0;
if coder.target('MATLAB')
% Executing in MATLAB, call MATLAB equivalent of
% C function foo
y = 'C:\Users\Tester\Desktop\Release\meter_app.bat';
else
% Executing in generated code, call C function foo
y = coder.ceval('execute');
end
end
Now when I try to generate c code using below mentioned MATLAB command. it generates c code in codegen/lib/callexecute folder.
codegen -config:lib callexecute execute.c execute.h
Now I have added MATLAB function block and I have added the above mentioned callexecute.m file in MATLAB function block. I have included header file and source file in simulation target. Now when I try to build the simulink model, it complaints implicit declaration of function 'execute' [-Werror=implicit-function-declaration] y = execute(); how do I avoid this error?
The non-deploy branch,
y = 'C:\Users\Tester\Desktop\Release\meter_app.bat';
should also
system(y)
You should pay attention to the data type of the return value: system() at the C level returns an int, but in the non-deploy branch you have it returning a character vector. Perhaps you should change the deploy branch to
y = system('C:\Users\Tester\Desktop\Release\meter_app.bat');
I recommend changing your declaration from
int execute();
to
int execute(void);
What care do I have to take in terms of path? I mean the code which is generated by means of codegen -config:lib callexecute execute.c execute.h is in different path and the files that are generated for rest of the simulink model is in different path.Do I have to place both of them in the same place?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!