[scilab-Users] mfprintf and vector

Stefan Du Rietz sdr at durietz.se
Wed Mar 18 18:27:42 CET 2009


On 2009-03-18 11:39, Rudy Magne wrote:
--------------------
> 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
> 
> 
I think you had a wrong format string in mfprintf - did you intend to 
have 10 positions and 4 decimals for each number?

In that case you should write:
"%10.4f %10.4f %10.4f \n"

Otherwise you will get 6 decimals (at least I get that) followed by 
".4". The parts of the format string that are not between "%" and "f" 
(where "f" is the conversion type) will be written exactly as in the 
format string. See help printf_conversion.

Did you first open the file to get fid?
fid = mopen(file, mode);
where file is a string with the pathname of your file and mode is a 
string which controls the access allowed to the file. See help mopen.

Regards
Stefan



More information about the users mailing list