|
[Sponsors] | |||||
Matlab:extract 10000 first lines from a file and write them in a new file |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|
|
#1 |
|
New Member
ron leco
Join Date: Sep 2012
Posts: 2
Rep Power: 0 ![]() |
Hello,
I have a file "myfile.txt" containing 45000 lines (it is not a matrix). I would like to read the 10000 first lines of my file ( the 10000 first lines define a matrix 3 columns ) and write it in a new txt file name "b.txt". How can I do that ? Please find below the bits of program I have already. Thank you in advance. Ronald fid=fopen(' myfile.txt,'r'); N = 10000 for n = 1:N fgetl(fid); end fprintf(fid,'%d %d %d\n','b'); fclose(fid); |
|
|
|
|
|
|
|
|
#2 |
|
Member
Gerhard Holzinger
Join Date: Feb 2012
Location: Austria
Posts: 60
Rep Power: 5 ![]() |
I did no testing, but shouldn't it look rather like this:
fidRead = fopen('myfile.txt'); fidWrite = fopen('b.txt'); for i=1:N line = fgetl(fidRead); writeLine(fidWrite); end fclose(fidRead); fclose(fidWrite); The code above is some mix of pseudo-code and actual code, but in principle I would do it like this. Open both files; read a line and write it right away, repeat this until you are done Close both files |
|
|
|
|
|
|
|
|
#3 |
|
New Member
ron leco
Join Date: Sep 2012
Posts: 2
Rep Power: 0 ![]() |
I wrote the program attached below and created a file b.txt with nothing in it in order to fill it with the help of the program ; it gives me the following error message :
"??? Attempted to access writeline(19); index out of bounds because numel(writeLine)=1 Error in ==>prog3 at 12 writeLine(fidWrite)" Why do I have this error ? Is it correct to create a file b.txt before launching the program. How can I declare b as a matrix 10000 X 3 in the program ? Thank you. Ron fidRead = fopen('myfile.txt','r'); fidWrite = fopen('b.txt','w'); N=10000 for i=1:N line = fgetl(fidRead); writeLine(fidWrite); end fclose(fidRead); fclose(fidWrite); |
|
|
|
|
|
|
|
|
#4 |
|
Senior Member
Bernhard
Join Date: Sep 2009
Location: Delft
Posts: 517
Rep Power: 9 ![]() |
Supposing you are using Linux, you can easily do this. It seems a bit useless to do it with Matlab.
Code:
$ head a.txt -n 10000 > b.txt |
|
|
|
|
|
|
|
|
#5 |
|
Member
Gerhard Holzinger
Join Date: Feb 2012
Location: Austria
Posts: 60
Rep Power: 5 ![]() |
as I wrote in my posting, my code contains some pseudocode. There is no function in Matlab called writeLine. Check the matlab help to see how to read and write from files, especially what those function expect as argument.
Depending on how you call fopen() b.txt will be created, will be overwritten or any things you write will be appended if this file exists. |
|
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Helyx-OS (GUI for SnappyHexMesh | elvis | OpenFOAM Native Meshers: snappyHexMesh and Others | 77 | March 5, 2013 02:22 |
| openfoam 1.6 on debian etch | romant | OpenFOAM Installation | 9 | May 6, 2010 02:26 |
| mesh file for flow over a circular cylinder | Ardalan | Main CFD Forum | 6 | April 17, 2010 23:40 |
| Write .def file from command line | Alejandro | CFX | 3 | September 11, 2005 18:14 |
| Results saving in CFD | hawk | Main CFD Forum | 16 | July 21, 2005 20:51 |