How to Pass String to System Call

4 ビュー (過去 30 日間)
Jessie Santiago
Jessie Santiago 2014 年 12 月 11 日
コメント済み: Jessie Santiago 2014 年 12 月 11 日
When trying to call a c program externally, I'm having a compiling error inside Matlab when trying to use fprintf to print out two float arrays that have been converted to type str through num2str.
I was forced to use fprintf since strcat removes spaces so instead of executing 'Emerge.exe (float variables)', it will output 'Emerge.exe(float variables)' with no space included in the DOS console.
Using fprintf combines the strings and includes the necessary spacing, but Matlab returns the error:
Error using system Argument must contain a string.
Error in GAforSharks (line 28) [~, scores] = system(fprintf(1,'%s %s %s ',program,num2str(population((n*2)-1,:)),num2str(population(n*2,:))),'-echo');
I used isstr() on the "fprintf(1,'%s %s %s ',program,num2str(population((n*2)-1,:)),num2str(population(n*2,:)))" to make sure it was type string and it is not, but I need to use fprintf to print the string and preserve the spacing. So I'm not sure how to go about passing a string with spaces to the system function.
TL;DR - Help please, I need to send the program name as well as an array of integers to console.
Regards,
Jessie S.

回答 (1 件)

David Sanchez
David Sanchez 2014 年 12 月 11 日
Use horzcat instead of strcat. horzcat keeps the blank spaces.
help horzcat
  1 件のコメント
Jessie Santiago
Jessie Santiago 2014 年 12 月 11 日
The solution was to use sprintf instead of fprintf. Thanks though.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by