How to skip the first line of a text file when reading it?

조회 수: 312 (최근 30일)
Jimmy
Jimmy 2011년 6월 8일
답변: Image Analyst 2023년 2월 4일
I have a file like this:
  1. DQ20091222000002.txt
  2. 2009-12-22 00:00:02--2009-12-23 00:00:12
  3. 3.4814
  4. 3.4814
  5. 3.4766
  6. 3.4814
I do not need the first two lines. How to skip them? By the way, the number list is extra, not included in my file.
  댓글 수: 1
Harsimran Singh
Harsimran Singh 2021년 5월 3일
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

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

답변 (6개)

Matt Tearle
Matt Tearle 2011년 6월 8일
Given how simple your file contents are, I'd use dlmread:
M = dlmread('filename.txt', ' ', 2, 0)
The last two arguments tell it to skip 2 rows (and no columns).
  댓글 수: 4
Jimmy
Jimmy 2011년 6월 8일
Nope.
Matt Tearle
Matt Tearle 2011년 6월 10일
Sorry, can't reproduce the problem. Perhaps some funky non-printing characters lurking in your file? One way to check for that is to do
foo = fileread('filename.txt');
then look at the values of double(foo). Values of 10 and/or 13 represent line breaks. Numbers should use characters 48-57 ('0'-'9') and 46 ('.') -- anything else would indicate a nonvalid number.
Is it always the last value? That is, what happens if you add a new value at the end of the file? Or insert a new value in the middle? That might help determine if it's the position or the specific value that happens to be at the end of your file.

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


Walter Roberson
Walter Roberson 2011년 6월 8일
The exact method will depend on which reading routine you are using. textscan() offers a HeaderLines option; some of the other routines offer similar options.
  댓글 수: 2
Jimmy
Jimmy 2011년 6월 8일
Could you show me how to use the textscan routine and the HeaderLines option for my case?
Walter Roberson
Walter Roberson 2011년 6월 8일
편집: per isakson 2017년 1월 26일
fid = fopen('DQ20091222000002.txt','rt');
indata = textscan(fid, '%f', 'HeaderLines',2);
fclose(fid);
yourdata = indata{1};

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


Sean de Wolski
Sean de Wolski 2011년 6월 8일
X = dlmread('ans68.txt','',2); %ans68.txt is your file.
doc dlmread
for more info

Abhishek Shahi
Abhishek Shahi 2018년 7월 1일
편집: Walter Roberson 2018년 7월 2일
temp=importdata('abc.dat');
abc=temp.data;
clear temp;

Parvez Akhtar
Parvez Akhtar 2019년 5월 10일
편집: Parvez Akhtar 2019년 5월 10일
Can anyone please help me out to skip lines from the end of the matrix data files as shown in below lines.
1. 23 45
2. 65 56
3. 64 87
4. 67 47
5. some additional comments at the end of the matrix.
In this data, we know the exact number of the last line, but for the bigger one, it is not easy to identify it. How can omit it while we are running the next read command?

Image Analyst
Image Analyst 2023년 2월 4일
allLines = readlines('ab.txt') % Gets every line into a cell.
% Skip first two lines
allLines = allLines(3:end) % Take only lines 3 and downward.

카테고리

Help CenterFile Exchange에서 Standard File Formats에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by