how to delete the empty row in the excel with matlab
Mostrar comentarios más antiguos
hi, as the title description, i want use matlab to delete the empty rows in the excel which has many rows, how to realize.
Respuesta aceptada
Más respuestas (2)
Sreeram
el 16 de Ag. de 2024
Hi Wenchao,
You can make use of the MATLAB function “cellfun” for such operations. You may read about it here:
If you want to delete all empty rows of an excel file, the command will be:
C(all(cellfun('isempty',C),2),:) = [];
where C is the input array which you can get by reading the Excel file.
Hope it helps.
3 comentarios
wenchao zhang
el 16 de Ag. de 2024
Sreeram
el 16 de Ag. de 2024
Thanks for informing me, Wenchao.
If you had used the "readTable" function to read the file, it might not have worked because of a mismatch in variable type of C passed to the "cellfun". The "readTable" detects the data type to determine how to import the data. Because the data in dataImport.xlsx was only numbers, the "readTable" might have set empty cells to NaN, and so the "isempty" check was insufficient.
It can be fixed by following the instructions given here to set the type to char. Here is how you can implement it:
opts = detectImportOptions('dataImport.xlsx');
opts = setvartype(opts, 'char');
data = readtable('dataImport.xlsx', opts);
data = table2cell(data);
data(all(cellfun('isempty',data),2),:) = [];
Hope this helps..
wenchao zhang
el 16 de Ag. de 2024
Hi,
Not sure about what you mean, do you want to:
- Delete a row that contain some empty cells (but the row still has non-empty cells) or,
- Delete the rows that contain only empty cells ?
Anyway,
In case 1: add MissingRule="omitrow" in the readtable function. Doing that will delete all rows that contain 1 or more empty cell.
In case 2: Should you have row name, add ReadRowNames = true in the readtable function. Otherwise, the readtable function automatically gets rid of empty rows.
1 comentario
wenchao zhang
el 16 de Ag. de 2024
Categorías
Más información sobre Spreadsheets en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
