0

Assuming that

outputTemp =

  2×1 cell array

    {122×1 string}
    {220×1 string}

finalOutput is a string array (342x1 string).

is there any way to do the following

outputTemp = arrayfun(@(x)someFunc(x), someInput, 'UniformOutput', false)';
finalOutput= [outputTemp{1}; outputTemp{2}];

in one line?

for the minimal example, someFunc can be a function that provides the names of the files in folders provided in someInput.

Gideon Kogan
  • 662
  • 4
  • 18

2 Answers2

0

Short answer: yes. Here is a MWE:

str1 = ["Test";"Test1";"42"]
str2 = ["new test";"pi = 3"]
C = {str1;str2}

ConCatStr = [C{1};C{2}];

This should answer the question regarding concatnation of string-arrays. Note that this is only possible with real strings (not with char-arrays). It is hard to tell, what you are doing beforehand as there are not details about getFilesFilt() and mainFolderCUBX.

EDIT MVE for the updated question

% function that returns a matrix
fnc = @(x)[x,1];
% anonymous function that returns a vector
fnc2 = @(x)reshape(fnc(x),2,1)

tmp = arrayfun(@(x)fnc(x), rand(10,1),'UniformOutput',false)

Answer: there is no proper way. However, you can do a little bit of fiddling and force everything into a single line (making the code ugly and less efficient)

tmp = arrayfun(@(x)fnc(x), rand(10,1),'UniformOutput',false);
out = reshape(cell2mat(tmp),numel(cell2mat(tmp)),1);

just replace the tmp with what is written with it.

max
  • 3,915
  • 2
  • 9
  • 25
  • My question was not phrased correctly, please review the question again and see if you can answer it. Your current answer is just a copy-paste of my code... – Gideon Kogan Jan 22 '20 at 07:51
  • I am still missing a MVE. Regarding the requirement of 1-line command: In gerneal, no. Though the type-fun functions can return two arguments (e.g. if you say `arrayfun(@max,array)` it will return the maximum and the index as a second argument), there is no proper way in phrasing a non-uniform output directly into a single output. – max Jan 22 '20 at 08:25
0

You can try the following code using cat() + subsref(), i.e.,

finalOutput= cat(1,subsref(arrayfun(@(x)someFunc(x), someInput, 'UniformOutput', false),struct('type', '{}', 'subs', {{:}})));

Example

S(1).f1 = rand(3,5);
S(2).f1 = rand(6,10);
S(3).f1 = rand(4,2);

cat(1,subsref(arrayfun(@(x) mean(x.f1)',S,'UniformOutput',false),struct('type', '{}', 'subs', {{:}})))

such that

ans =

   0.89762
   0.53776
   0.42440
   0.25272
   0.58197
   0.34503
   0.40259
   0.41792
   0.43527
   0.53974
   0.49976
   0.63342
   0.36539
   0.58541
   0.57042
   0.60914
   0.60851
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81