Filter löschen
Filter löschen

How to Pass String to System Call

7 Ansichten (letzte 30 Tage)
Jessie Santiago
Jessie Santiago am 11 Dez. 2014
Kommentiert: Jessie Santiago am 11 Dez. 2014
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.

Antworten (1)

David Sanchez
David Sanchez am 11 Dez. 2014
Use horzcat instead of strcat. horzcat keeps the blank spaces.
help horzcat
  1 Kommentar
Jessie Santiago
Jessie Santiago am 11 Dez. 2014
The solution was to use sprintf instead of fprintf. Thanks though.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by