[scilab-Users] mfprintf and vector

harishankar ramachandran hsr at ee.iitm.ac.in
Wed Mar 18 07:15:06 CET 2009


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



-- 
Dr. Hari Ramachandran, Professor, 332B ESB, EE Dept, IIT-Madras
Interests: Nonlinear Optics, Nonlinear Waves, Plasma Physics, Particle 
           Simulations, Computational Algorithms, Linux.
Off: 91-44-2257-4421                    Fax: 91-44-2257-0120
Res: 91-44-2663-1863             Home Email: omkarbharathi at gmail.com



More information about the users mailing list