mercredi 1 mai 2019

MATLAB: For Loop and IF to Evaluate a Regular Expression String for Deletion

I am trying to feed 3 character arrays, dataset, into a for loop and use the if statements to evaluate a regular expression to determine if the selected string should be deleted. I'm getting and error: deleteFromSet1 = contains(dataset, deleteString1), line 14.

My code output goal is to delete apples, squash, and dark from the character array, dataset.

Before running the script, the character array is: applesgrapesorangesspinachsquashbroccolilemondarkcitrus

After running the script, the character should output: grapesorangesspinachbroccolilemoncitrus

Here is my code:

fruits = ['apples', 'grapes', 'oranges'];
veggies = ['spinach', 'squash', 'broccoli'];
tea = ['lemon', 'dark', 'citrus'];

dataSet = [fruits, veggies, tea];


for i = dataSet

deleteString1 = 'apples';
deleteString2 = 'squash';
deleteString3 = 'dark';

deleteFromSet1 = contains(dataset, deleteString1);
deleteFromSet2 = contains(dataset, deleteString2);
deleteFromSet3 = contains(dataset, deleteString3);


    if deleteFromSet1 == contains(dataset, deleteString1);
            goDeleteString1 = regexprep(dataset, deleteFromSet1, '')
     end
    if deleteFromSet2 == contains(dataset, deleteString2);
            goDeleteString2 = regexprep(dataset, deleteFromSet2,'')
    end
     if deleteFromSet3 == contains(dataset, deleteString3);
            goDeleteString3 = regexprep(dataset, deleteFromSet3,'')
     end
end

Aucun commentaire:

Enregistrer un commentaire