CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Matlab:extract 10000 first lines from a file and write them in a new file (https://www.cfd-online.com/Forums/openfoam-programming-development/106704-matlab-extract-10000-first-lines-file-write-them-new-file.html)

ronald September 5, 2012 21:20

Matlab:extract 10000 first lines from a file and write them in a new file
 
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);

GerhardHolzinger September 6, 2012 03:53

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

ronald September 6, 2012 19:25

writeLine(fidWrite)
 
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);

Bernhard September 7, 2012 02:33

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

GerhardHolzinger September 7, 2012 02:34

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.

ronald September 24, 2013 11:23

Scilab-colorbar
 
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)")


All times are GMT -4. The time now is 01:31.