sprintf to cell array

조회 수: 62 (최근 30일)
Sven
Sven 2016년 9월 1일
답변: Enoch23 2019년 12월 12일
How can I make sprintf output a cell array, with a nice vectorized notation?
When I have:
C = {'a','b'}
Output = sprintf('Letter:%s\n',C{:})
It becomes a char of length 18. Simple workaround would be a loop, maybe scan with a separator, or a solution with cellfun and anonymous function should work as well.
But is there not a nice straightforward vectorized solution to this? My first guess would have been something like:
Output{:} = sprintf('Letter:%s\n',C{:})
Which does not work.
Thanx in advance Sven

채택된 답변

Stephen23
Stephen23 2016년 9월 1일
편집: Stephen23 2016년 9월 1일
Read this blog, it shows lots of ways of doing this, and also introduces the fastest method of all (which is undocumented, so use it at your own risk):
EDIT some alternative for strings. Thinking outside the box and is very fast:
strrep(['Letter:%s',10],'%s',{'a','b'})
Much slower:
strcat('Letter:',{'a','b'},char(10))
cellfun(@(s)sprintf('Letter:%s\n',s),{'a','b'},'UniformOutput',false);
  댓글 수: 2
Sven
Sven 2016년 9월 1일
This was the direction I was looking for especially sprintfc, thanx alot. Though I have not yet managed to get it running the way I'd liked.
Output = sprintfc('Letter:%s \n',C{:})
makes an error and
Output = sprintfc('Letter:%s \n',[C{:}])
only gives Letter:ab as a single element of the cell array, not the {Letter:a, Letter:b} I hoped for.
Guillaume
Guillaume 2016년 9월 1일
If all the strings in your cell array are the same length, then
sprintfc('some string: %s', vertcat(C{:}))
would work. If not, unless you pad the shorter strings so all the lengths match, you cannot use sprintfc. My understanding of sprintfc (bearing in mind that there's absolutely no documentation) is that it is designed to transform numerical input (not cell arrays or char arrays input) into cell arrays of strings.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Enoch23
Enoch23 2019년 12월 12일
In the link shared by Stephen Cobeldick, there is an edit:
Addendum: Starting in R2016b, the main functionality of sprintfc (excluding sprintfc‘s 3rd [isImaginary] input flag, and its 2nd/3rd output args [errorMsg and isLeft]) is included in the new fully-documented/supported function compose.
I thought I would post it here, as that is exactly what I was looking for. Maybe others looking for the same answer will come across this page.

Guillaume
Guillaume 2016년 9월 1일
In my opinion, the cleanest way to do what you want would be:
C = {'a', 'b'};
output = cellfun(@(l) sprintf('Letter:%s', l), C, 'UniformOutput', false)
It may not be the fastest, but it clearly shows the intent and does not involve using undocumented functions that may change behaviour in the next release or just disappear altogether (or just reformat your hard drive given this one particular undocumented input, who knows?).
Not that if it's just single characters that you're passing to sprintf (as the 'Letter' string would suggest, then %c may be more appropriate. In that case, you also don't need to use a cell array:
C = 'ab';
output = arrayfun(@(l) sprintf('Letter@%c', l), C, 'UniformOutput', false)

Perry Orthey
Perry Orthey 2016년 9월 1일
You could use strsplit:
C = {'a','b'};
Output = strsplit(sprintf('Letter:%s \n',C{:}));
Although this gives you one empty cell at the end.
  댓글 수: 1
Sven
Sven 2016년 9월 1일
Thanx for this, though it actually is kind of the answer I thought of with scan, had strsplit in my mind before.

댓글을 달려면 로그인하십시오.

카테고리

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