How to delete specific texts on CSV file with MATLAB?

1 回表示 (過去 30 日間)
HSukas
HSukas 2021 年 9 月 25 日
回答済み: TED MOSBY 2024 年 10 月 19 日
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!

回答 (1 件)

TED MOSBY
TED MOSBY 2024 年 10 月 19 日
Hi HSukas,
Steps to Filter Data
  1. Read the CSV File: Use readcell to read the data if it's structured like a cell array.
  2. Define the Criteria for Removal: Create a list of keywords or phrases that you want to filter out.
  3. Filter the Data: Use logical indexing to remove rows that contain any of the specified phrases.
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:

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by