- If you want to compare CSV files then use a reputable file comparison tool (MATLAB has one inbuilt).
- Do not use Excel if a particular file format and/or data needs to be preserved.
readtable behaves different after editing CSV?
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
iwantrugs
el 8 de Dic. de 2021
I have one CSV file output from some software. The original CSV file (one attached without 'edited' appended) has some header lines which I don't care about and a column of times which I do care about.
The column of times in the original file is incorrect since some stuff happened during the acquisition. I have edited the said CSV file to fix the errors. When I use the code below on the edited file I get the error at the final line saying Input data must be a real, numeric array. If I run the same code on the unedited CSV file it runs fine and behaves as expected; outputting a vector of the original times in seconds, which is what I want. I'm really unsure why this is happening - I've not changed anything else in the CSV files aside from adding a few times, but the number format of all the cells afaik is the same in both CSV files.
ons_csv = readtable('S17.csv');
ons_times = ons_csv(:,1);
ons_times = table2array(ons_times);
ons_seconds = seconds(ons_times);
1 comentario
Stephen23
el 24 de Dic. de 2021
Editada: Stephen23
el 24 de Dic. de 2021
"I've not changed anything else in the CSV files aside from adding a few times, but the number format of all the cells afaik is the same in both CSV files."
No, it isn't. The problem is that you used Excel to alter a CSV text file. Every time you open and then save a CSV file with Excel, it will completely re-write the file using what Excel thinks is a CSV format. There is nothing preventing Excel from changing the file format or data formats, e.g. delimiters, dates, string quotations, numbers, etc. Even if you think you only changed some small part of the CSV file, Excel makes zero effort to keep the "rest" of the file unchanged.
We learn two lessons from this:
CSV files are just text: if you only need to make a few changes then you should use a reputable text editor:
Respuesta aceptada
Cris LaPierre
el 24 de Dic. de 2021
Editada: Cris LaPierre
el 24 de Dic. de 2021
I think the issue is more obvious if you open the csv file in a text editor instead of Excel.
The original file does not have the same number of commas in the header rows. Readtable is able to therefore detect where the data starts, and skip the header.
In the edited file, Excel adds commas so every row has the same number of columns. Readtable is not able to automatically detect the header anymore, so starts importing the data from row 1. This is leading to your error.
The fix is to tell MATLAB the number of header lines when using the edited file.
One more thing - Excel also has changed the formatting of your time (drops the hours - again, check in a text editor). This means MATLAB is no longer able to autodetect the datatype, so that column is being read in as a character array. You will need to manually correct the data type and set the input format so that it is imported correctly.
Once you've done that, there is no need to convert your table to an array to get the time values. See the Access Data in a Table documentation page.
Here's how I might import your file.
opts2 = detectImportOptions("S17edit.csv",'NumHeaderLines',8);
opts2 = setvartype(opts2,"StartTime","duration");
opts2 = setvaropts(opts2,"StartTime",'InputFormat','mm:ss.S');
ons_csv = readtable('S17edit.csv',opts2)
% Obtain time in seconds
ons_seconds = seconds(ons_csv.StartTime)
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Spreadsheets en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!