Matlab Coder with Command Line Parameters

I have completed the coderand example. My next step is to create an executable that accepts command line inputs.
Here is my MATLAB code:
function c = Calculator(whichcalc) %#codegen
fprintf('%s\n',whichcalc);
if whichcalc=="pressure"
c = "Your calc is the pressure calculator";
elseif whichcalc=="temperature"
c = "Your calc is the temperature calculator";
else
c = "No idea which calculator.";
end
fprintf('%s\n',c);
end
And the associated command to run the application:
screenshot.png
I've created the C++ source code using Matlab coder & then used Visual Studio to compile the code using a local ubuntu vm.
Where have I gone wrong?

2 comentarios

Ryan Livingston
Ryan Livingston el 13 de Feb. de 2019
Can you share the C++ main function that you're using? The generated code doesn't read from the command line, so your main function needs to take argv and forward that to the generated entry-point function.
Here's an example that shows taking arguments in the main function and passing them to the generated code:
Specifically look at main in main.c that reads from argv.
Thank you very much for the example. That looks like it will help tremendously.
I did finally get things working. I'll post here in case you have some suggestions or in case others benefit from my hard-earned gains.
Proof:
screenshot2.png
Code (main.cpp):
static void main_Calculator(string whichcalc)
{
/* Initialize function 'Calculator' input arguments. */
/* Initialize function input argument 'whichcalc'. */
/* Call the entry-point 'Calculator'. */
Calculator(whichcalc);
}
int main(int argc, char *argv[])
{
/* Initialize the application.
You do not need to do this more than one time. */
Calculator_initialize();
/* Invoke the entry-point functions.
You can call entry-point functions multiple times. */
main_Calculator(argv[1]);
/* Terminate the application.
You do not need to do this more than one time. */
Calculator_terminate();
return 0;
}
Code (Calculator.cpp):
void Calculator(string whichcalc)
{
int minSize;
int i;
cell_wrap_0 validatedHoleFilling[1];
bool equal;
char varargin_1_data[40];
static const char cv0[8] = { 'p', 'r', 'e', 's', 's', 'u', 'r', 'e' };
char c_Value_data[39];
static const char cv1[36] = { 'Y', 'o', 'u', 'r', ' ', 'c', 'a', 'l', 'c', ' ',
'i', 's', ' ', 't', 'h', 'e', ' ', 'p', 'r', 'e', 's', 's', 'u', 'r', 'e',
' ', 'c', 'a', 'l', 'c', 'u', 'l', 'a', 't', 'o', 'r' };
static const char cv2[11] = { 't', 'e', 'm', 'p', 'e', 'r', 'a', 't', 'u', 'r',
'e' };
static const char cv3[25] = { 'N', 'o', ' ', 'i', 'd', 'e', 'a', ' ', 'w', 'h',
'i', 'c', 'h', ' ', 'c', 'a', 'l', 'c', 'u', 'l', 'a', 't', 'o', 'r', '.' };
static const char cv4[39] = { 'Y', 'o', 'u', 'r', ' ', 'c', 'a', 'l', 'c', ' ',
'i', 's', ' ', 't', 'h', 'e', ' ', 't', 'e', 'm', 'p', 'e', 'r', 'a', 't',
'u', 'r', 'e', ' ', 'c', 'a', 'l', 'c', 'u', 'l', 'a', 't', 'o', 'r' };
if (whichcalc.length() != 0) {
minSize = whichcalc.length();
for (i = 0; i < minSize; i++) {
validatedHoleFilling[0].f1.data[i] = whichcalc[i];
}
validatedHoleFilling[0].f1.data[whichcalc.length()] = '\x00';
minSize = whichcalc.length() + 1;
for (i = 0; i < minSize; i++) {
varargin_1_data[i] = validatedHoleFilling[0].f1.data[i];
}
printf("%s\n", &varargin_1_data[0]);
fflush(stdout);
}
/* fprintf('%s\n',whichcalc); */
minSize = whichcalc.length();
if (minSize >= 8) {
minSize = 8;
}
if (minSize == 0) {
equal = (whichcalc.length() == 8);
} else {
i = 0;
while ((i + 1 <= minSize) && (!(whichcalc[i] != cv0[i]))) {
i++;
}
if (i + 1 == minSize + 1) {
equal = (whichcalc.length() == 8);
} else {
equal = (whichcalc[i] == cv0[i]);
}
}
if (equal) {
minSize = 36;
for (i = 0; i < 36; i++) {
c_Value_data[i] = cv1[i];
}
} else {
minSize = whichcalc.length();
if (minSize >= 11) {
minSize = 11;
}
if (minSize == 0) {
equal = (whichcalc.length() == 11);
} else {
i = 0;
while ((i + 1 <= minSize) && (!(whichcalc[i] != cv2[i]))) {
i++;
}
if (i + 1 == minSize + 1) {
equal = (whichcalc.length() == 11);
} else {
equal = (whichcalc[i] == cv2[i]);
}
}
if (equal) {
minSize = 39;
for (i = 0; i < 39; i++) {
c_Value_data[i] = cv4[i];
}
} else {
minSize = 25;
for (i = 0; i < 25; i++) {
c_Value_data[i] = cv3[i];
}
}
}
for (i = 0; i < minSize; i++) {
validatedHoleFilling[0].f1.data[i] = c_Value_data[i];
}
validatedHoleFilling[0].f1.data[minSize] = '\x00';
minSize++;
for (i = 0; i < minSize; i++) {
varargin_1_data[i] = validatedHoleFilling[0].f1.data[i];
}
printf("%s\n", &varargin_1_data[0]);
fflush(stdout);
}
Modifications necessary:
  • Accept arguments in main
  • Pass argument to main_calculator
  • Update main_calculator to accept string (it was previously accepting a custom-made data type that MATLAB Coder generated)
  • Update Calculator to use strings using the typical length() function and string indexing ([i])
  • Update function definitions in related .h files

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre MATLAB Coder en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 4 de Feb. de 2019

Comentada:

el 13 de Feb. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by