Import CSV from nth row. n is variable across files

2 ビュー (過去 30 日間)
Bhargav
Bhargav 2024 年 2 月 11 日
編集済み: Alexander 2024 年 2 月 14 日
Hi,
How do I generalize importing a csv file. The csv file contains lot of information before the data headers and the actual data starts. For example for one CSV file the headers ( variablesnames) start at 100th row and row 101 is data. It might not be the same for another csv file. Also the data ends at a certain row, but in the first column there is addtional row which has end of file (EOF) and the all the others columns are empty.
Whenever I use
readtable
function, it treats the headers as NaN.
Also, the common header between all the CSVs that I am using is 'time' which might be 100th row in one csv file but be in another row in another csv file.
The first column is time stamps but I can treat that as a data point so it is easier for me to plot stacked plot.
Thanks !
Bhargav
  6 件のコメント
Bhargav
Bhargav 2024 年 2 月 14 日
Should I create a new thread ?
Alexander
Alexander 2024 年 2 月 14 日
編集済み: Alexander 2024 年 2 月 14 日
No, everything is OK. @Voss has moved your answer to a comment.

サインインしてコメントする。

回答 (1 件)

Alexander
Alexander 2024 年 2 月 14 日
編集済み: Alexander 2024 年 2 月 14 日
I think (the specialists here might correct me) you don't get a ready to use function, that solves your problem. Here a suggestion:
% https://de.mathworks.com/matlabcentral/answers/2080816-import-csv-from-nth-row-n-is-variable-across-files
datlength = 60000; % Adjust it to your needs
Data = char(zeros(1,datlength));
fid = fopen('example_csv.csv');
while (feof(fid) == 0)
dyData = pad(fgetl(fid),datlength);
Data = [Data; dyData(1:datlength)];
end
fclose(fid);
Data=deblank(Data); % Reduce Data to the max.
% Now you have your csv in "Data". You can parse it to your needs line by
% line.
If I get some more information I'll try to help further on. But not today (UTC+2 ;-).

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by