[scilab-Users] mfprintf and vector

Rudy Magne rudy.magne at shom.fr
Wed Mar 18 11:39:22 CET 2009


harishankar ramachandran a écrit :
> On Tuesday 17 March 2009 15:17, Rudy Magne wrote:
>   
>> Dear All,
>>
>> Is is possible to write in a text file a (1,nx) vector with 3 components
>> per lines (as it is available in matlab or fortran) with a defined
>> format (see below)?
>>
>> ex: fprintf(fid,"%10f.4 %10f.4 %10f.4 \n", vector(1:nx))
>>
>> I have tried with mfprintf or mtlb_fprintf without succes.  fprintfMat
>> would have done the job but it is then not possible to write before and
>> after the written matrix.
>>
>> Regards,
>> Rudy
>>     
>
> Here is a way of doing it with the fortran i/o commands. It is not as elegant 
> as fprintfMat and fscanfMat, since it does not handle arbitrary number of 
> preceding header lines. But it does what you want. Ofcourse, I don't know 
> whether fortran i/o is supported any longer in scilab v5.x (I am using v4.1)
>
> // Using fortran i/o
>
> // Open the file for operations. Delete existing file if required
> f=file('open',"junk.dat","unknown")
> // write out a header
> header=["First line of header";"Second line of header"];
> write(f,header,'(a)')
> // write out the matrix
> A=[1 2 3;4 5 6;7 8 9];
> write(f,A,'(3(1x,1p,g15.8))');
> // close the file
> file('close',f);
>
> // open the file for reading
> f=file('open',"junk.dat","old")
>
> // read in the header (need to know number of lines)
> header1=read(f,2,1,"(a)");
>
> // read in the matrix 
> B=read(f,-1,3,"(3g16.0)");
>
> // close the file
> file('close',f);
>
> // print out header and matrix
> disp(header1)
> disp(B);
>
> Regards
>
> hari ramachandran
>
>
>
>   
Thanks Hari,
It works fine.
Rudy


-- 
-------------------------
Rudy Magne
SHOM/HOM/REC        
Vagues et domaine littoral  
13 rue du Chatellier, 29609 BREST Cedex
Tel: (33) 2 98 149 936 
Fax: (33) 2 98 221 864 




More information about the users mailing list