remove last line data file

11 次查看(过去 30 天)
A MATLAB script is made to load data from a .xyz file into the workspace. The datafile consists of three columns of numbers, the number of lines varies per file, about 800 to 1000 lines. However, the file has also text which I would like to ignore. I would like to make a script that automatically removes/ignores the text in the file. How to get rid of the last line of the file? 1 1 1 2 4 0 6 8 0 . . . . . . . . . tekst Thanks for your answer, - A

采纳的回答

Geoff Hayes
Geoff Hayes 2014-12-10
Antoine - if you are interested in removing the last line from the file (which includes non-numeric text), then try using importdata which seems to ignore the last line if it isn't numeric. For example, if your text file, test.txt, has the following contents
12 12 12
13 13 13
14 14 14
15 15 15
a final line with non-numeric text
then
data = importdata('test.txt')
returns
data =
12 12 12
13 13 13
14 14 14
15 15 15
Try the above and see what happens!
  2 个评论
Antoine van Hirtum
Antoine van Hirtum 2014-12-12
The first 14 lines should also be removed/ignored. Can I still use importdata. How does the code look like now?
Geoff Hayes
Geoff Hayes 2014-12-12
编辑:Geoff Hayes 2014-12-12
Check the documentation for this function and look at the headerLinesIn input parameter. You could try using it as follows
fileData = importdata('test.txt',' ' ,14);
where ' ' indicates that row element is using the space character as the delimiter, and 14 is the number of header lines. So if your text file now looks like
a
b
c
d
e
f
g
h
i
j
k
l
m
n
12 12 12
13 13 13
14 14 14
15 15 15
a final line with non-numeric text
then calling the above line of code will set fileData to be a structure with two fields - the numeric data, and the alphabetic textdata. So
fileData.data =
12 12 12
13 13 13
14 14 14
15 15 15
Try experimenting with the importdata function.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by