MATLAB Answers

Translated by

このページのコンテンツは英語から自動翻訳されています。自動翻訳をオフにする場合は「<a class="turn_off_mt" href="#" onclick="window._kiq.push(['set', { 'event': 'Turn off MT' }]);">ここ</a>」をクリックしてください。

0

Making an executable with input arguments using codegen

Yuval Geyari さんによって質問されました 2018 年 9 月 4 日
最新アクティビティ Yuval Geyari さんによって 編集されました 2018 年 9 月 5 日
I'm trying to compile a Matlab code using codegen and making an executable. The thing is that after compilation, I can't seem to successfully call the app from matlab along with the relevant input. I'll explain:
To make things simple, let's assume that the purpose of my program is to create a '.txt' file named 'my_text.txt' and print an input string inside, like so:
function PrintMyString(input_str) %#codegen
fid = fopen('my_text.txt','w');
fprintf(fid,'My input string is: %s',input_str);
fclose(fid);
end
Now, I tried to compile the code above using the following script:
str = 'pre_defining_the_input_variable_by_example';
codegen -config config_obj PrintMyString -args {str}
when config_obj is the code generation configuration defined somewhere earlier.
The compilation was successful, but as I mentioned before, when I tried to activate the program it didn't go as planned. I tried various options for calling, such as:
system(['PrintMyString.exe ',str]);
The text file was created, but no input string was printed (all that appeared was the line "My input string is: ").
I also tried adding a main function that calls the original PrintMyString function, like so:
function main() %#codegen
str = 'some_string';
PrintMyString(str);
end
I compiled it like that:
str = 'pre_defining_the_input_variable_by_example';
codegen -config config_obj main PrintMyString -args {str}
And that worked, but obviously it doesn't help because the input string has to be sent to the program from outside.
Any ideas? What am I doing wrong?

  0 件のコメント

サインイン to comment.

1 件の回答

回答者: Titus Edelhofer
2018 年 9 月 4 日
 採用された回答

Hi,
it's probably not a question of the code generation but your main file: you compile to an executable, so probably your config_obj carries the information about the main file that you have to have. This main file should be able to take string input and pass it to the generated C code.
Maybe you can share your main file and we can take a look.
Titus

  5 件のコメント

Hi Titus, you really helped me! But still something doesn't work.
I am now trying to compile the code using the Coder App, which seems to be more convenient than using codegen.
The new main.c file is pretty simple:
#include "PrintMyString.h"
#include "PrintMyString_initialize.h"
#include "PrintMyString_terminate.h"
int main( int argc , char* argv[] ) {
PrintMyString_initialize();
PrintMyString( argv[1] );
PrintMyString_terminate();
return 0;
}
After creating the main.c file, I've attempted to compile the code, unsuccessfully. In the Error Report I've noticed the following line:
gmake: *** No rule to make target 'D:/Programs/ProjectFolder.lib', needed by 'D:/Programs/ProjectFolder/PrintMyString.exe'. Stop.
When 'D:/Programs/ProjectFolder' is the path to the directory ProjectFolder in which the original PrintMyString.m file is located, along with the new main.c file.
What is this '.lib' file mentioned in the error line? I cannot seem to figure it out. Any ideas?
Hi,
this is what I did: I used the coder app as well and
  • specified myself the input to be char of size 1x:Inf(to have strings of undefined length as input).
  • Selected executable
  • Generated code
  • Copied the generated example\main.c, main.h to mymain.c, mymain.h
  • Made some changes (here the changed lines):
#include "mymain.h" // instead of #include "main.h"
static void main_PrintMyString(const char* str) // add str as input
input_str = emxCreateWrapper_char_T(str, 1, strlen(str)); // rows=1, cols = length of the input string
main_PrintMyString(argv[1]); // second input is the input parameter (first is the name of the executable)
  • and added mymain.c on the "More Settings"->Custom Code->Additional Source files
  • and generated code again
  • and if everything works fine, call the executable :)
Titus
It works great, thank you! Althougth I used a different input argument in the last line you mentioned:
main_PrintMyString(input_str);
And also, how exactly did you know to use the emxCreateWrapper_char_T function? It wasn't in the original example main.c file.

サインイン to comment.



Translated by