MATLAB: Is there a simple approach to adding spaces before and after every number in a cell array, where each cell is a combination of numbers and letters

cell arraysMATLAB

Hello,
I have an [m x n] cell in the range of thousands. For simplicity, let's deal with a 2×3 cell instead. The elements are as follows:
{[123xswqe412]} {[xasdak12399a8s]} {[123123xq13]}
{[qasoasf1231]} {[asd11skka13551]} {[127u8adkja]}
Can you please advise on how I can make it so that the output is as follows:
{[123 xswqe 412]} {[xasdak 12399 a 8 s]} {[123123 xq 13]}
{[qasoasf 1231]} {[asd 11 skka 13551]} {[127 u 8 adkja]}
Any help would be appreciated!
Thanks in advance!

Best Answer

I don't know why I took the time to do this and my function separating the words is super ugly but I think you want something like this:
% string
str = '123xswqe412ew3';
% regular expression to separate string from number
expr = '\d+|\D+';
%create cell with strings
mycell = cell(50, 1);
mycell(:) = {str};
% perform function on cell
mycell(:) = cellfun(@(x)doRegExp(x, expr), mycell, 'UniformOutput', false);
% function to separate strings
function f = doRegExp(str, expr)
ind = regexp(str, expr);
f = str(ind(1):ind(2)-1);
for i = 2:length(ind)-1
a = str(ind(i):ind(i+1)-1);
f = [f, ' ', a];
end
f = [f, ' ', str(ind(i+1):end)];
end