- Read the CSV File: Use readcell to read the data if it's structured like a cell array.
- Define the Criteria for Removal: Create a list of keywords or phrases that you want to filter out.
- Filter the Data: Use logical indexing to remove rows that contain any of the specified phrases.
How to delete specific texts on CSV file with MATLAB?
    3 views (last 30 days)
  
       Show older comments
    
Hi everyone
I have a CSV file that contains some spefisic values of the product. 
I need to delete data that includes "Number%," and "Under $50,", "$50-$100,", "$100-$200,". Is there a way to that on matlab? 
An example data is also given below:    
    {'Rug,Transitional,2' x 2'11",5'3" x 7'3",7'1" x 10'2",Charcoal,Light Gray,Beige,Cream,5%,30%,10%,Under $50,$50-$100,$50-$100'}
Thanks in advance!
0 Comments
Answers (1)
  TED MOSBY
 on 19 Oct 2024
        Hi HSukas, 
Steps to Filter Data 
I wrote an example code for the same: 
% Step 1: Read the CSV file 
data = readcell('your_file.csv'); % Replace 'your_file.csv' with your actual filename 
% Step 2: Define the phrases to remove 
phrasesToRemove = {"Number%", "Under $50", "$50-$100", "$100-$200"}; 
% Step 3: Initialize a logical index for rows to keep 
rowsToKeep = true(size(data, 1), 1); % Assume all rows are to be kept initially 
% Loop through each phrase and update the logical index 
for i = 1:length(phrasesToRemove) 
    % Create a logical array that is true for rows containing the phrase 
    containsPhrase = contains(data, phrasesToRemove{i}, 'IgnoreCase', true); 
    rowsToKeep = rowsToKeep & ~any(containsPhrase, 2); % Keep rows that do not contain the phrase 
end 
% Step 4: Filter the data 
filteredData = data(rowsToKeep, :); 
% (Optional) Step 5: Write the cleaned data to a new CSV file 
writecell(filteredData, 'cleaned_data.csv'); % Replace with desired output filename 
% Display the cleaned data 
disp(filteredData); 
Hope this helps! 
Here is the documentation for “contains” and “readcell’ function of MATLAB: 
0 Comments
See Also
Categories
				Find more on Text Files in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
