Unpack arrayfun output into different variables

조회 수: 2 (최근 30일)
Raphael Alhadef
Raphael Alhadef 2019년 11월 10일
댓글: Raphael Alhadef 2019년 11월 10일
I am trying to use sprintf on an array of strings and store the output strings in individual variables, but I am failing on the syntax.
I tried:
a = ["f","g","h"];
tf = @(x) sprintf('%s%s','C:\',x);
[m,n,o] = arrayfun(tf,a,'UniformOutput',false);
I get an error reading:
Error using sprintf
Too many output arguments.
Error in Untitled>@(x)sprintf('%s%s','C:\',x)
The ouptut is a 1x3 cell, so I hope this should be possible. Thank you

채택된 답변

the cyclist
the cyclist 2019년 11월 10일
No, it is not possible.
It is true that the output of arrayfun in this case is a 1x3 cell array. And you may feel that it is obvious that MATLAB should then parse that into your three variable. But MATLAB actually cannot "know" that that is what you mean. Specifically, it cannot know which of the following you want for your output:
  1. m = 1x1 cell; n = 1x1 cell; o = 1x1 cell
  2. m = 1x3 cell; n = empty; o = empty
  3. m = 1x2 cell; n = 1x1 cell; o = empty
  4. ... and so on ...
You'll need to parse that yourself. (I would suggest that maybe keeping them in a cell array rather than separate variables might actually be the better design.)
  댓글 수: 1
Raphael Alhadef
Raphael Alhadef 2019년 11월 10일
I understand, thank you for the clarification.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by