How to Pass String to System Call

4 views (last 30 days)
Jessie Santiago
Jessie Santiago on 11 Dec 2014
Commented: Jessie Santiago on 11 Dec 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.

Answers (1)

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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!