1

I have a cell array containing strings and numbers. The strings contain double quotes that are creating problems for me in MATLAB. I found a solution to replace the double quotes with single quotes in this link. However, the solution only appears to work when all the cell contents are strings, and I have numbers in some of mine. I don't want to convert the numbers I have to strings because I will be doing calculations with them later in the work flow.

Here is an example of the type of cell array I have and what I have tried so far:

myCell1 = {'';'"some words"';64;23;'"words"'};
myCell2={'';'more words';'more stuff';46;15};
Cell={myCell1, myCell2}

% Attempt at a for loop to only apply regexprep to strings
for i = 1 : numel(Cell)
    for r = 1:numel(Cell{i})
        if ischar(Cell{i}{r})
            newCell{i}{r} = regexprep(Cell, '^"|"$', '');
        end
    end
end

Any help figuring out how to apply regexprep() to strings in this mixed setup is much appreciated!

0

2 Answers 2

1

It looks to me like you almost have it. I've made some modifications to your code and it seems to work. Changed lines are those with comments.

for i = 1 : numel(Cell)
    for r = 1:numel(Cell{i})
        if ischar(Cell{i}{r})
            newCell{i}{r} = regexprep(Cell{i}{r}, '^"|"$', ''); %// changed "Data" to "Cell"
        else %// added this branch...
            newCell{i}{r} = Cell{i}{r}; %// ... to keep cells containing numbers unchanged
        end
    end
end
Sign up to request clarification or add additional context in comments.

Comments

1

You don't have a choice when dealing with regular expressions. You must convert the numbers to strings if you want to use numbers for regular expressions. However, Luis Mendo's approach is more simpler as you just need another if statement in your logic branches. That method is fine too, and more simpler.

If you don't want to do this and want to use numbers in regular expressions, we can keep track of which elements in your cell array were numbers to begin with, convert those numbers to strings, use regexprep, then convert these entries back to numbers when you're done.

You can use cellfun to check which elements in your cell array are numbers. Then, convert these elements to strings, do your processing, then convert back. First check to see if each element is a number by isnumeric. After this, you can use num2str to convert the numbers into strings. After, use str2num to convert the strings back to numbers. In other words, do this:

myCell1 = {'';'"some words"';64;23;'"words"'};
myCell2={'';'more words';'more stuff';46;15};
Cell={myCell1, myCell2};
newCellArray = cell(1,numel(Cell)); %// Place output cell array here

for i = 1 : numel(Cell)
    %// Extract i'th cell
    cel = Cell{i};
    %// Which ones are numbers?
    whichAreNumbers = cellfun(@isnumeric, cel);

    %// Convert those numbers into strings
    outStrings = cellfun(@num2str, cel(whichAreNumbers), 'uni', false);
    %// Assign back into cell
    cel(whichAreNumbers) = outStrings;

    %// Apply regexprep now
    newCell = regexprep(cel, '^"|"$', '');

    %// Convert the numbers back
    outVals = cellfun(@str2num, newCell(whichAreNumbers), 'uni', false);
    newCell(whichAreNumbers) = outVals;

    %// Place in master output cell array
    newCellArray{i} = newCell;       
end

The output at this point is:

>> celldisp(newCellArray)

newCellArray{1}{1} =

   ''


newCellArray{1}{2} =

some words


newCellArray{1}{3} =

 64



newCellArray{1}{4} =

 23



newCellArray{1}{5} =

words


newCellArray{2}{1} =

 ''


newCellArray{2}{2} =

more words


newCellArray{2}{3} =

more stuff


newCellArray{2}{4} =

  46


newCellArray{2}{5} =

  15

As you can see, the numbers are still preserved but the quotes for each of the strings are removed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.