1

This character array uses a for loop to index through the (basketArray) data, uses IF to check for subscript (00) in data, and if true deletes the (00) subscript. This is achieved with the output of:

rmsubscript = applesorangespineapplespinachbroccoliturnips.

Next step is where I am having the problem: how do I get the character array output back into its declared x and y containers, formatted by a column and separated as:

x =

apples
oranges
pineapple

y =

 spinach
 broccoli
 turnips

Here is my code:

x = ['00apples', 'oranges', '00pineapple']
y = ['00spinach', 'broccoli', '00turnips']

basketArray = [x, y]
pattern = '00';
subscript = contains(basketArray, pattern);
for k = basketArray
    if subscript == contains(basketArray, pattern);
            rmsubscript = regexprep(basketArray, '00', ''),                                                    
    end
end
1
  • Realistically, you don't, because concatenating them together into character vectors is destructive (and unnecessary). Store your words in cell arrays, regexprep works on them natively. Commented May 2, 2019 at 14:30

1 Answer 1

2

Use string arrays or cell arrays to contain your words. Note that x and y are probably not what you're expecting:

x =

    '00applesoranges00pineapple'

y =

    '00spinachbroccoli00turnips'

regexprep works natively on both cell and string arrays:

x = {'00apples', 'oranges', '00pineapple'};
y = {'00spinach', 'broccoli', '00turnips'};

basket = [x; y];
new_basket = regexprep(basket, '00', '')

and

x = ["00apples", "oranges", "00pineapple"];
y = ["00spinach", "broccoli", "00turnips"];

basket = [x; y];
new_basket = regexprep(basket, '00', '')

Both produce the desired result:

new_basket = 

  2×3 string array

    "apples"     "oranges"     "pineapple"
    "spinach"    "broccoli"    "turnips"  
Sign up to request clarification or add additional context in comments.

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.