我知道num2str的使用方法是:
tested = 3.53;
X=[資料集的平均值是:', num2str(tested)]。
disp(X)。
輸出。資料集的平均值是。3.53
我的問題是,我如何使用類似的方法來顯示一個矢量?
基本上,我有一個由Concentration=[3.5, 3.8, 3.2, 3.9]給出的向量,并希望列印出類似于"資料集的濃度是。3.5, 3.8, 3.2, 3.9"/code>
uj5u.com熱心網友回復:
明顯的答案是:
disp(['the concentrations of the data set are: ' mat2str(Concentration)])
但是它并沒有給出你所需要的輸出:
資料集的濃度是。[3.5 3.8 3.2 3.9]
這里有兩個黑客選項:
。fprintf('The concentrations of the data set are: '/span>)
fprintf('%.1f, ', Concentration)
fprintf('
')
輸出:
資料集的濃度是。3.5, 3.8, 3.2, 3.9。
注意,中間的fprintf對任何數量的元素都有效。洗掉了后面的空格和逗號。
另外:
outstr = strrep(mat2str(Concentration), ' '/span>, ' , ')
disp(['資料集的濃度是。' outstr(2:end-1) ])
輸出:
資料集的濃度是。3.5, 3.8, 3.2, 3.9。
uj5u.com熱心網友回復:
你可以使用strjoin來建立字串,并使用fprintf將其包含在一個句子中。
conc = [3.5, 3.8, 3.2, 3.9] 。
concStr = arrayfun( @num2str, conc, 'uni', 0 ); % 轉換為字符單元。
% 用分隔符', '連接所有的值,然后用fprintf使用這個單一的字串。
fprintf( '資料集的濃度是。%s', strjoin( concStr, ', ' ) 。
uj5u.com熱心網友回復:
試試新的compose()函式! 它就像一個面向陣列的sprintf()。
strjoin(compose("%0.2f", Concentration), " , ")
給你:
ans =
"3.50, 3.80, 3.20, 3.90"
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/326923.html
標籤:
