import csv file and skip the first 5 lines that contain text
Mostra commenti meno recenti
Hi,
I have 1000 files of which the first few lines all look like the ones below. There are 11 columns of data.
What I want to do is import these csv files but not the first 5 lines that contain text. The remaining part only contains numbers.
I think I have to work with textscan and headerlines, but I am not sure how to.
Thank you for your help!
StimName,"Subject","sequence","Date","Time","FirstTR","SessionNo","Trial","trseq","StimDuration","sound"
Fc1_incr,"ai","PupilToPupil_ai",#2011-03-29#,#1899-12-30 13:44:26#,19,3,1,19,4200,"dum"
StartPosX,"StartPosY","dum","dum","dum","dum","dum","dum","dum","dum","dum"
.5,.5,0,0,0,0,0,0,0,0,0
SystemTime,"MediaTime","Valid","GazeX","GazeY","LeftX","LeftY","LeftPupil","RightX","RightY","RightPupil"
13,0,3,.5333887,.5088381,.4567934,.5429853,3.403872,.6443502,.5561797,3.531345
31,0,3,.4986517,.4756647,.4566537,.5422695,3.449122,.6440785,.5554679,3.480466
47,0,3,.4954451,.4924191,.456565,.5420035,3.41274,.6440116,.5553075,3.527733
104,0,3,.5075845,.4980071,.4565195,.541966,3.402078,.6439878,.5554283,3.512885
104,0,3,.5068237,.4639177,.4564521,.5419917,3.34627,.643941,.5556482,3.48911
Risposte (3)
Oleg Komarov
il 16 Apr 2011
EDIT typo:
fid = fopen('filename')
data = textscan(fid,'%f%f%f%f%f%f%f%f%f%f%f','Delimiter',',','HeaderLines',5)
fid = fclose(fid);
16 Commenti
Mariska Kret
il 16 Apr 2011
Oleg Komarov
il 16 Apr 2011
In filename you're supposed to put a string indicating the full path of the file you want to import.
Mariska Kret
il 26 Apr 2011
Oleg Komarov
il 26 Apr 2011
Please recheck my code, I forgot last time to put the fid.
Mariska Kret
il 26 Apr 2011
Oleg Komarov
il 26 Apr 2011
Did you supply the full correct name to the file you want to open?
Example:
fid('C:\Users\Oleg\Desktop\myfile.txt'
Mariska Kret
il 26 Apr 2011
Oleg Komarov
il 26 Apr 2011
http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers
Mariska Kret
il 27 Apr 2011
Oleg Komarov
il 27 Apr 2011
No, I don't agree with mohammed.
Add to the call to texscan this option:
data = textscan(...,'CollectOutput',1);
data = data{:};
Now you *should* be able to interpolate.
Post eventually whole error messages if you get any.
Mariska Kret
il 27 Apr 2011
Oleg Komarov
il 27 Apr 2011
Uhm, never used Matlab 6. Then just use cell2mat.
Mariska Kret
il 2 Lug 2011
Oleg Komarov
il 2 Lug 2011
You need to use a loop (http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F)
% to retrieve all the csv files in a folder
f = dir('*.csv');
f = {f.name};
% LOOP
for n = f
csvread(n{:})
end
Mariska Kret
il 4 Lug 2011
Oleg Komarov
il 4 Lug 2011
Use your data = csvread(...,5); in the loop.
data = cell(numel(f),1);
for n = 1:numel(f)
data{n} = csvread(f{n},5);
end
To consolidate (If all files with same num of cols):
data = cat(1,data{:});
mohammed
il 26 Apr 2011
0 voti
I advice you to convert your files to excel, then you can use the following code
inputs = xlsread('fileName.xls', 1, 'A6:D1000');
where A6 the first cell and D1000 the last cell
Harsimran Singh
il 3 Mag 2021
0 voti
use this link, it works perfectly for me:
Option Explicit
Sub FixCsvFiles()
Dim SelectFolder As String
Dim csvFiles As Variant
Dim csvWb As Workbook
Dim x As Integer
'browse for folder with csv files
On Error GoTo FixCsvFiles_Error
SelectFolder = GetFolder("c:\")
Application.ScreenUpdating = False
'Check user did not cancel folder selection
If SelectFolder = "" Then
MsgBox "No Folder Selected - Cannot continue", vbCritical
End
End If
SelectFolder = SelectFolder & "\"
csvFiles = Dir(SelectFolder & "*.csv")
Do While csvFiles <> ""
Set csvWb = Workbooks.Open(SelectFolder & csvFiles)
Rows("1:2").Delete
x = x + 1
csvWb.Close True
csvFiles = Dir
Loop
Application.ScreenUpdating = True
MsgBox "A total of " & CStr(x) & " files processed", vbInformation
On Error GoTo 0
Exit Sub
FixCsvFiles_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure FixCsvFiles of Module2"
End Sub
Function GetFolder(strPath As String) As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "BROWSE TO FOLDER LOCATION WITH CSV FILES"
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function
Categorie
Scopri di più su Standard File Formats in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!