Delete entire rows based on one condition

Hello,
I am a beginner in Matlab. I have a table that has 7 columns and 240 rows.
I need to delete entire rows based on the following condition:
1. if a value of column 7 is superior than 1000 and inferior than 100 => delete this row
I have attached an image of how the datset looks like. Please help!

Respuestas (1)

Luna
Luna el 5 de Dic. de 2019
Editada: Luna el 6 de Dic. de 2019
Hi,
Try this:
data = num2cell(randi(5000,240,7)); % sample random data in a cell array
my_Table = cell2table(data); % convert cell array to table
new_table = my_Table(~(my_Table.data7 > 1000 | my_Table.data7 < 100),:); % filter and remove

11 comentarios

Image Analyst
Image Analyst el 5 de Dic. de 2019
Try again -- he want to delete those rows, not extract (keep) them.
Khayroon Suleyman
Khayroon Suleyman el 5 de Dic. de 2019
It doesnt work :(
Luna
Luna el 6 de Dic. de 2019
Made a small change. Can you check again?
Khayroon Suleyman
Khayroon Suleyman el 6 de Dic. de 2019
Still doesn't do what I want it to unfortunately
Luna
Luna el 6 de Dic. de 2019
Editada: Luna el 6 de Dic. de 2019
Can you share your code and we make updates on it?
my_Table -> your table name (which is CourseworkFi....)
data7 -> your 7th column name. (which is Targetexp...)
Use (.) dot notation between.
Khayroon Suleyman
Khayroon Suleyman el 6 de Dic. de 2019
There is no issue with the code, but it just doesn't remove the rows that have values in column 7 that are greater than 1000 and less than 100.
I used this code but it said: Unrecognized table variable name 'TargetexpRT'.
data = num2cell(randi(5000,240,7)); % sample random data in a cell array
CourseworkFinalDATAEXCEL = cell2table(data); % convert cell array to table
new_table = CourseworkFinalDATAEXCEL (~(CourseworkFinalDATAEXCEL.TargetexpRT > 1000 | CourseworkFinalDATAEXCEL.TargetexpRT < 100),:); % filter and remove
I think we have some misunderstanding here.
  1. You should define variable names while using cell2table. If you do not define them, Matlab autogenerates variable names for you and most probably the variable name will not be TargetexpRT. You should go to your Matlab's Workspace and double click the table to see what are your variable names.
  2. How did you import your CourseworkFinalDATAEXCEL ? when you run the line CourseworkFinalDATAEXCEL = cell2table(data); this only overrides my randomly created data to your CourseworkFinalDATAEXCEL so you should make an import again.
Here is some tips how to import from Excel:
readtable you should put 'ReadRowNames' input as true to read first column of your excel file as variable names.
Here in your case with your variable names:
CourseworkFinalDATAEXCEL = readtable('CourseworkFinalDATAEXCEL.xlsx','ReadRowNames',true);
new_table = CourseworkFinalDATAEXCEL(~(CourseworkFinalDATAEXCEL.TargetexpRT > 1000 | CourseworkFinalDATAEXCEL.TargetexpRT < 100),:);
Khayroon Suleyman
Khayroon Suleyman el 6 de Dic. de 2019
It says:
Error using readtable (line 216)
Unable to open file 'CourseworkFinalDATAEXCEL.xlsx' as a workbook. Check that the file exists, read access is available, and the
file is a valid spreadsheet file.
Luna
Luna el 9 de Dic. de 2019
Editada: Luna el 9 de Dic. de 2019
Use the fullfile path and name together for example:
readtable('C:\Users\Desktop\...\CourseworkFinalDATAEXCEL.xlsx','ReadRowNames',true);
The path is where your excel file is.
Ayush Jain
Ayush Jain el 29 de Mayo de 2022
Thanks it worked

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 5 de Dic. de 2019

Comentada:

el 29 de Mayo de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by