CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > OpenFOAM Programming & Development

Matlab:extract 10000 first lines from a file and write them in a new file

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree1Likes
  • 1 Post By Bernhard

Reply
 
LinkBack Thread Tools Display Modes
Old   September 5, 2012, 21:20
Default Matlab:extract 10000 first lines from a file and write them in a new file
  #1
New Member
 
ron leco
Join Date: Sep 2012
Posts: 3
Rep Power: 4
ronald is on a distinguished road
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);
ronald is offline   Reply With Quote

Old   September 6, 2012, 03:53
Default
  #2
Senior Member
 
Gerhard Holzinger
Join Date: Feb 2012
Location: Austria
Posts: 166
Rep Power: 14
GerhardHolzinger will become famous soon enoughGerhardHolzinger will become famous soon enough
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
GerhardHolzinger is offline   Reply With Quote

Old   September 6, 2012, 19:25
Default writeLine(fidWrite)
  #3
New Member
 
ron leco
Join Date: Sep 2012
Posts: 3
Rep Power: 4
ronald is on a distinguished road
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);
ronald is offline   Reply With Quote

Old   September 7, 2012, 02:33
Default
  #4
Senior Member
 
Bernhard
Join Date: Sep 2009
Location: Delft
Posts: 790
Rep Power: 12
Bernhard is on a distinguished road
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
mturcios777 likes this.
Bernhard is offline   Reply With Quote

Old   September 7, 2012, 02:34
Default
  #5
Senior Member
 
Gerhard Holzinger
Join Date: Feb 2012
Location: Austria
Posts: 166
Rep Power: 14
GerhardHolzinger will become famous soon enoughGerhardHolzinger will become famous soon enough
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.
GerhardHolzinger is offline   Reply With Quote

Old   September 24, 2013, 11:23
Default Scilab-colorbar
  #6
New Member
 
ron leco
Join Date: Sep 2012
Posts: 3
Rep Power: 4
ronald is on a distinguished road
Hello,
I would like to plot 2 graphs with 1 colorbar. The colour bar wouldn't not vary but the graph would vary with respect to the colorbar. For instance the values vary from 0 to 1600 for the 1st plot, from 0 to 1200 in the second plot. The colorbar would vary from 0 to 1600, but the 2nd plot would have the colours corresponding to the maximum values being 1200 in the colorbar thus clearer than he maximum of the colorbar.
Please find attached below my program in scilab.
At the moment I am able to plot each graph but with a new colorbar each time varying from the min to the max of the values of the plot. If I put 1600 for the max of the colorbar, the corresponding colour (1600) is also displayed on the graph even if the max on the graph is 1200. Thank you for your help.
aa1=gca();aa1.x_location="bottom"aa1.y_location="right"xset("colormap",jetcolormap(64))aa=min(y3)b=max(y3)colorbar(aa,b)Sgrayplot(x,y,y3)xtitle(" T distribution","x (meter)","y (meter)")
ronald is offline   Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Helyx-OS (GUI for SnappyHexMesh elvis OpenFOAM Native Meshers: snappyHexMesh and Others 176 July 10, 2015 16:50
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


All times are GMT -4. The time now is 09:35.