How to put cell array in sprintf?
古いコメントを表示
Data is in cell array: {1,2,3,4,5} sprintf('%d,%d,%d,%d,%d ',???);
採用された回答
その他の回答 (3 件)
Dasharath Gulvady
2015 年 5 月 29 日
1 投票
a={1,2,3,4,5};
sprintf('%d,%d,%d,%d,%d',a{:});
Doug
2015 年 5 月 29 日
0 投票
mycell = {1,2,3,4,5};
s = sprintf('%d%d%d%d%d', mycell{:})
Jan Siegmund
2020 年 8 月 25 日
編集済み: Jan Siegmund
2020 年 8 月 31 日
If using multiple different cells:
%% Edited
% var = {'a','b','c','d','e'};
% num = {1,2,3,4,5};
% s = strjoin(cellfun(@(v,n)sprintf('%s = %d',v,n),var,num,'UniformOutput',false),'; ');
Stephen's comment for a proper solution
2 件のコメント
Stephen23
2020 年 8 月 25 日
Simpler and more efficient:
>> tmp = [var;num];
>> fprintf('%s = %d\n',tmp{:})
a = 1
b = 2
c = 3
d = 4
e = 5
Jan Siegmund
2020 年 8 月 31 日
編集済み: Jan Siegmund
2020 年 8 月 31 日
Oh, when sprintf and fprintf reported:
>> s = sprintf('%s = %d\n',var,num)
Error using sprintf
Function is not defined for 'cell' inputs.
I thougth sprintf and fprintf were not defined for cell arrays.
Edited: (And I did not think about the cell unpacking {:}
Anyways, yours is definitely the better solution!
カテゴリ
ヘルプ センター および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!