How do I combine character strings and numeric arrays using fprintf?

11 ビュー (過去 30 日間)
Joel Nowlin
Joel Nowlin 2017 年 11 月 8 日
回答済み: Stephen23 2017 年 11 月 8 日
Basically need help with the syntax, I need to have the first column of the table a string array of characters and the next three a matrix of numerical values

回答 (2 件)

KSSV
KSSV 2017 年 11 月 8 日
str = 'Hi this is demo:' ; % a string
val = rand(1,3) ;
fprintf('%s %f %f %f\n',str,val)
  2 件のコメント
Joel Nowlin
Joel Nowlin 2017 年 11 月 8 日
I tried this format, but matlab evaluates the second character string with %f, say str=["Bob" "Sue" "Jim" "Phil"] matlab returns "Bob NaN NaN NaN NaN" Is there any way to prioritize the first input argument (str) to get matlab to evaluate the whole string before moving on to the numerical array? Sorry if I'm bad at explaining this
KSSV
KSSV 2017 年 11 月 8 日
str=["Bob" "Sue" "Jim" "Phil"] ;
fprintf([repmat('%s ',1,length(str)),'\n'],str)

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


Stephen23
Stephen23 2017 年 11 月 8 日
>> C = {'Anna','Bob','Charly','Diane'};
>> M = rand(4,3);
>> D = [C(:),num2cell(M)]';
>> fprintf('%8s %8f %8f %8f\n',D{:})
Anna 0.713433 0.187407 0.376554
Bob 0.140956 0.777929 0.433123
Charly 0.888480 0.623920 0.942841
Diane 0.734345 0.404552 0.725598
>>

カテゴリ

Help Center および File ExchangeLaTeX についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by