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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by