- 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?
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
TED MOSBY
2024-10-19
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!