Here are a couple of options:
Option 1: If your data has a consistent pattern of commas and parentheses across rows, you can actually parse it quite easy with a regex. The downside is that if your pattern changes, you have to change the regex. But it's also quite fast (even for very large cell arrays):
str = {'(0, 0, 1540.4, (true, (121.96, 5)), 5.7068, 1587.0)';
'(0, 0, 1537.5, (true, (121.93, 6)), 5.7068, 1587.0)';
'(0, 0, 1537.5, (true, (121.93, 3)), 5.7068, 1587.0)';
'(0, 0, 1537.5, (true, (121.93, 4)), 5.7068, 1587.0)';
'(0, 0, 1537.5, (true, (121.93, 5)), 6.0965, 1587.0)';
'(0, 0, 1535.2, (true, (121.9, 6)), 6.0965, 1587.0)';
'(0, 0, 1535.2, (true, (121.9, 3)), 6.0965, 1587.0)';
'(0, 0, 1535.2, (true, (121.9, 4)), 6.0965, 1587.0)';
'(0, 0, 1535.2, (true, (121.9, 5)), 6.3782, 1587.0)';
'(0, 0, 1532.3, (true, (121.87, 6)), 6.3782, 1587.0)'};
tokens = regexp(str, ['^\(([-\d\.]+), ' ... % Column 1
'([-\d\.]+), ' ... % Column 2
'([-\d\.]+), ' ... % Column 3
'(\(\w+, \([-\d\.]+, [-\d\.]\)\)), ' ... % Column 4
'([-\d\.]+), ' ... % Column 5
'([-\d\.]+))'], ... % Column 6
'tokens', 'once');
str = vertcat(tokens{:});
disp(str);
And the result for this example:
'0' '0' '1540.4' '(true, (121.96, 5))' '5.7068' '1587.0'
'0' '0' '1537.5' '(true, (121.93, 6))' '5.7068' '1587.0'
'0' '0' '1537.5' '(true, (121.93, 3))' '5.7068' '1587.0'
'0' '0' '1537.5' '(true, (121.93, 4))' '5.7068' '1587.0'
'0' '0' '1537.5' '(true, (121.93, 5))' '6.0965' '1587.0'
'0' '0' '1535.2' '(true, (121.9, 6))' '6.0965' '1587.0'
'0' '0' '1535.2' '(true, (121.9, 3))' '6.0965' '1587.0'
'0' '0' '1535.2' '(true, (121.9, 4))' '6.0965' '1587.0'
'0' '0' '1535.2' '(true, (121.9, 5))' '6.3782' '1587.0'
'0' '0' '1532.3' '(true, (121.87, 6))' '6.3782' '1587.0'
Note that I used the pattern [-\d\.]+
to match an arbitrary number which may have a negative sign or decimal point.
Option 2: You can use regexprep
to repeatedly remove pairs of parentheses that don't contain other parentheses, replacing them with whitespace to maintain the same size string. Then, find the positions of the commas in the final processed string and break up the original string using these positions. You won't have to change the regex for each new pattern of commas and parentheses, but this will be a little slower than the above (but still only taking a second or two for arrays of up to 15,000 cells):
% Using raw str from above:
str = cellfun(@(s) {s(2:end-1)}, str);
tempStr = str;
modStr = regexprep(tempStr, '(\([^\(\)]*\))', '${blanks(numel($0))}');
while ~isequal(modStr, tempStr)
tempStr = modStr;
modStr = regexprep(tempStr, '(\([^\(\)]*\))', '${blanks(numel($0))}');
end
commaIndex = regexp(tempStr, ',');
str = cellfun(@(v, s) {mat2cell(s, 1, diff([1 v numel(s)+1]))}, commaIndex, str);
str = strtrim(strip(vertcat(str{:}), ','));
disp(str);
This gives the same result as Option 1.