How to remove rows in Table

5 views (last 30 days)
I have an excel file with 20000+ rows & 10 columns. I need only data of few rows (ex. 650 to 1200, 3000 to 3600) present in the table along with its related columns and the remaining rows must be deleted. Please help me with a simple code for this task.

Accepted Answer

Scott MacKenzie
Scott MacKenzie on 17 May 2021
Edited: Scott MacKenzie on 17 May 2021
You want to read 10 columns of data in rows 650 to 1200. There is no need to read the entire file. Try this...
inFile = 'yourfile.xlsx';
dataRange = 'A650:J1200';
worksheet = 1;
T = readtable(inFile, 'FileType', 'spreadsheet', 'Sheet', worksheet, 'Range', dataRange);
  3 Comments
Scott MacKenzie
Scott MacKenzie on 17 May 2021
There are many ways to read and manipulate data. You could read the two sections of data separately, then concatenate the two matrices:
T1 = readtable(inFile, 'FileType', 'spreadsheet', 'Sheet', worksheet, 'Range', 'A650:J1200');
T2 = readtable(inFile, 'FileType', 'spreadsheet', 'Sheet', worksheet, 'Range', 'A3000:J3600');
T = [T1; T2];
Or, you could read all the data, then reassign T to itself while specifying only the rows of interest:
T = readtable(inFile, 'FileType', 'spreadsheet', 'Sheet', worksheet);
T = T([650:1200 3000:3600],:);
For exporting data to a .csv file, use writetable, for example...
writetable(T, 'newfile.csv');
For all the details and examples, I suggest you study the documentation for readtable and writetable. Good luck.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!