How to delete specific texts on CSV file with MATLAB?

조회 수: 2 (최근 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 CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by