Is there a possibility to join the strings out of cells in cells?

1 view (last 30 days)
Dear Matlab-community,
i have many cells in cells, here is a simple example: Testfile = {'String1', 'String2';{'String3';'String4'}, {'String5';'String6'}};
I would like to have somthing like this: first cell: String1.String3,String1.String4 second cell: String2.String5,String2.String6
Is there a fuction, which helps to extract the cell information easier, then looping over all cells?
  1 Comment
Jan
Jan on 16 Apr 2018
What exactly is "first cell: String1.String3" Please post valid Matlab code to define the wanted output uniquely.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 16 Apr 2018

I think that's what you want. If not as per Jan's comment, use valid matlab syntax to define the desired output

cellfun(@(s, c) compose('%s.%s', s, string(c)), Testfile(1, :), Testfile(2, :), 'UniformOutput', false)

Requires R2016b or later for compose and string.

More Answers (1)

Jan
Jan on 16 Apr 2018
Edited: Jan on 16 Apr 2018

There is no standard function to solve this special problem. Therefore a loop is the best and easiest idea.

I do not understand, what you want as output, but perhaps:

T  = {'String1',             'String2'; ...
     {'String3';'String4'}, {'String5';'String6'}};
nT = size(T, 2);
R  = cell(1, nT);
for iT = 1:nT
   R{iT} = sprintf('%s.%s,%s.%s', T{1, iT}, T{2, iT}{1}, T{1, iT}, T{2, iT}{2});
end

Result:

R = {'String1.String3,String1.String4', ... 
     'String2.String5,String2.String6'}

Maybe you want another result, but the method should be equivalent.

Tags

Community Treasure Hunt

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

Start Hunting!