import csv file and skip the first 5 lines that contain text
Mostrar comentarios más antiguos
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
Respuestas (3)
Oleg Komarov
el 16 de Abr. de 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 comentarios
Mariska Kret
el 16 de Abr. de 2011
Oleg Komarov
el 16 de Abr. de 2011
In filename you're supposed to put a string indicating the full path of the file you want to import.
Mariska Kret
el 26 de Abr. de 2011
Oleg Komarov
el 26 de Abr. de 2011
Please recheck my code, I forgot last time to put the fid.
Mariska Kret
el 26 de Abr. de 2011
Oleg Komarov
el 26 de Abr. de 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
el 26 de Abr. de 2011
Oleg Komarov
el 26 de Abr. de 2011
http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers
Mariska Kret
el 27 de Abr. de 2011
Oleg Komarov
el 27 de Abr. de 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
el 27 de Abr. de 2011
Oleg Komarov
el 27 de Abr. de 2011
Uhm, never used Matlab 6. Then just use cell2mat.
Mariska Kret
el 2 de Jul. de 2011
Oleg Komarov
el 2 de Jul. de 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
el 4 de Jul. de 2011
Oleg Komarov
el 4 de Jul. de 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
el 26 de Abr. de 2011
0 votos
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
el 3 de Mayo de 2021
0 votos
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
Categorías
Más información sobre Standard File Formats 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!