Data is in cell array: {1,2,3,4,5} sprintf('%d,%d,%d,%d,%d ',???);

 채택된 답변

Jos (10584)
Jos (10584) 2015년 5월 29일

0 개 추천

Use comma-separated list expansion. And you need to specify the format only once (see help fprintf)
A = {1 2 3 4}
sprintf('%d ',A{:})

추가 답변 (3개)

Dasharath Gulvady
Dasharath Gulvady 2015년 5월 29일

1 개 추천

 a={1,2,3,4,5};
 sprintf('%d,%d,%d,%d,%d',a{:});
Doug
Doug 2015년 5월 29일

0 개 추천

mycell = {1,2,3,4,5};
s = sprintf('%d%d%d%d%d', mycell{:})
Jan Siegmund
Jan Siegmund 2020년 8월 25일
편집: Jan Siegmund 2020년 8월 31일

0 개 추천

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

Simpler and more efficient:
>> tmp = [var;num];
>> fprintf('%s = %d\n',tmp{:})
a = 1
b = 2
c = 3
d = 4
e = 5
Jan Siegmund
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에 대해 자세히 알아보기

질문:

2015년 5월 26일

편집:

2020년 8월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by