Find cells that start with text and combine it with next cell
    6 views (last 30 days)
  
       Show older comments
    
I have a cell array like:
    {'Test1'}
     {'10v'}
    {'20va'}
    {'30v'}
    {'50v'}
    {'65v'}
    {'Test2'}
    {'80v} 
    {100v}
I want to find cells that start with text and combine that with next cell and store in same variable. Output should be something like:
    {'Test1 10v'}
    {'20va'}
    {'30v'}
    {'50v'}
    {'65v'}
    {'Test2 80v'} 
    {100v}
0 Comments
Accepted Answer
  Stephen23
      
      
 on 13 Sep 2019
        
      Edited: Stephen23
      
      
 on 13 Sep 2019
  
      >> C = {'Test1';'10v';'20va';'30v';'50v';'65v';'Test2';'80v';'100v'}
C = 
    'Test1'
    '10v'
    '20va'
    '30v'
    '50v'
    '65v'
    'Test2'
    '80v'
    '100v'
>> X = cellfun('isempty',regexp(C(:),'^\D','once'));
>> Y = cumsum([1;X(1:end-1)]);
>> F = @(x){sprintf(' %s',C{x})};
>> Z = strtrim(accumarray(Y,(1:numel(C)).',[],F))
Z = 
    'Test1 10v'
    '20va'
    '30v'
    '50v'
    '65v'
    'Test2 80v'
    '100v'
2 Comments
  Stephen23
      
      
 on 13 Sep 2019
				>> Z = accumarray(Y,(1:numel(C)).',[],@(x){strjoin(C(x))})
Z = 
    'Test1 10v'
    '20va'
    '30v'
    '50v'
    '65v'
    'Test2 80v'
    '100v'
More Answers (1)
  David Hill
      
      
 on 13 Sep 2019
        Cell array (C), combined cell array (c).
i=1;
j=1;
while i<=size(C,2)
    a=cell2mat(C{i})-57;
    if a(1)>0
        c{j}=[cell2mat(C{i}),' ',cell2mat(C{i+1})];
        i=i+2;
    else
        c{j}=C{i};
        i=i+1;
    end 
    j=j+1;
end
0 Comments
See Also
Categories
				Find more on Characters and Strings in Help Center and File Exchange
			
	Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

