필터 지우기
필터 지우기

Import multiple .csv files with different of variables and rows

조회 수: 6 (최근 30일)
Benjamin Azrieli
Benjamin Azrieli 2020년 11월 11일
편집: Jeremy Hughes 2020년 11월 18일
I have a large amount of .csv files that have basically the same format as the one I attached, except the number/type of variables ( and number of data points is different depending on the file. The function I attached is able to read in the current file I have and assign column headers, which Matlab gathered by reading the 3rd line of the .csv when I used the 'Import Data' then 'Generate Function' buttons. My question is how can I make this function general so that it reads the file, then creates a table with the same column headers as the current file. I do not need any of the information in the top line of the .csv's, I only need the 3rd line (variables) and below (data).
There is a space between the 3rd and 5th lines of the .csv's, which I would like not to change manually because I have so many files, which may affect reading variable names using alternative methods.
I tried removing the variable naming from line 95 of the importfile function I created, and then using textscan() to read the 3rd line of the .csv to use as variable names, but for some reason textscan stops after the first column (Date-time); most likely because it is recognizing the 'time' cell as NaN.
I have also tried importdata(), but all it did was import the very first cell of the .csv.
Any help is appreciated, thank you

답변 (2개)

Mathieu NOE
Mathieu NOE 2020년 11월 13일
hello
following your first idea, I modified the import function so that it can extract the varaible names from the 3rd line of the header
so far it works , but if you test it with a csv file with another number of columns or differently organized we will have an issue
with formatSpec = '%s%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%C%f%C%f%f%f%f%C%f%C%f%C%f%f%f%f%f%f%f%C%[^\n\r]'; that also need to be addressed in case of change of dimensions of the csv file
maybe in a 2nd attempt ?
all the best
  댓글 수: 2
Benjamin Azrieli
Benjamin Azrieli 2020년 11월 17일
편집: Benjamin Azrieli 2020년 11월 17일
Thank you for trying - I ended up just cutting out the problem column (first column) in all the csv files and then used readtable(), which worked perfectly. Hoping someone else comes along who knows how to skip columns when reading in csv's.
Mathieu NOE
Mathieu NOE 2020년 11월 17일
ok
glad you have a solution that works

댓글을 달려면 로그인하십시오.


Jeremy Hughes
Jeremy Hughes 2020년 11월 17일
편집: Jeremy Hughes 2020년 11월 18일
More recent versions of MATLAB generate code with the Import Tool which used the Import Options and readtable. The code is simpler, and has more configuration options available.
I added a line to sub-select variables to the base generated code.
opts.SelectedVariableNames = [...]
The code should still work in some older releases (those where import options are available).
  댓글 수: 4
Jeremy Hughes
Jeremy Hughes 2020년 11월 18일
I can see it was fixed in R2019a. I'll make a report, and I've made some changes to work around the issue.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by