Ely
Ely

Reputation: 115

Matlab: trasform to cell of strings

Problem: I have postfix (137x25) cell that contain cell of char (Ex postfix(6,8)=postfix{6,8}{1,1}<1x3char>). I want to trasform it in (137x25) cell of char.

Postfix is created in this way:

for l=1:25
    [matches(:,l), postfix(:,l)] = regexp(semanticTrajCompact(1,4).TrajCompact,sprintf('%d%d(.*)',digits{1}(l),digits{2}(l)),'match','once','tokens');
end

I have tried different solutions:

Solution 1

numIndex = cellfun('isclass', postfix, 'double');
  tmpStr = sprintf('%g;', postfix{numIndex});
  postfix(numIndex) = dataread('string', tmpStr, '%s', 'delimiter', ';');

Solution 2

postfix(cellfun(@isempty,postfix))={''};

Solution 3

postfix(cellfun(@isnumeric, postfix)) =  cellfun(@(x) sprintf('%.5f', x), postfix(cellfun(@isnumeric, postfix)), 'UniformOutput', false)

Solution 4

I try to use

char(postix)

Anyone of this solution trasform postfix in a (137x25 array of char). Can you give me other ideas?

Upvotes: 0

Views: 53

Answers (2)

Ely
Ely

Reputation: 115

I have solved the problem also in this way

for k=1:25
    for j=size(postfix,1)
        if(~isempty(postfix{j,k}))
            postfix{j,k}=char(postfix{j,k}{1,1});
        end
    end
end

postfix(cellfun(@isempty,postfix))={''};

Upvotes: 0

Jonas
Jonas

Reputation: 74940

You want to loop over every element of the cell array postfix, and replace each element of the cell array (which is a cell array itself) by its contents. Using cellfun to replace the loop, you therefore write:

postfix = cellfun(@(x)x{1}, postfix, 'UniformOutput', false);

Upvotes: 1

Related Questions