Writing Data to text file gives error,unknown parameter

hi All
I am trying to write some parameter to a py file as follows
Let's say I have the m-file 1 that contains the parameters like p=2,,,, q=3*p ,,,,,, and tf=1
m-file 2 is a function like : mvar(m) , I always assign m=1,
file 1 runs m-file 2 that contains these lines:
fid = fopen('myparams.py', 'w');
fprintf(fid,'p1 = %d\n',p1);
fprintf(fid,'q = %0.12f\n',q); fprintf(fid,'tf= %0.12f\n',tf);
to write them on the third file myparams , it writes the first 2 but for the last one, tf , it gives error , while it is introduced in the same way also I dont know the difference of %d\n with %12f\n
the error is
Function is not defined for 'tf' inputs
thank you very much

 Respuesta aceptada

Florian
Florian el 14 de Oct. de 2014
Hi,
%d is used when you want to write an integer (1,2,3,...)
%f and all derivative (%12f,...) are used to write float numbers (specify the precision, add spaces,..)
see the help of fprintf.
and I just try the code bellow, it works.
p = 2;
q = 3*p;
tf = 1 ;
fid = fopen('myparams.py', 'wt');
fprintf(fid,'p1 = %d\n',p);
fprintf(fid,'q = %d\n',q);
fprintf(fid,'tf= %d\n',tf);
fclose(fid)
The error message you get tells that ft is not defined. you should check if your variable is really created.

13 comentarios

farzad
farzad el 14 de Oct. de 2014
Thank you , I have written the same, only that the first three lines in a separate file and introduced the parameters , also you have written 'wt' instead of 'w' , what's the difference? it still didn't work for me
I also don't want integer , but float
farzad
farzad el 14 de Oct. de 2014
After Running and getting the Error , I type tf and I get a result , exactly what I have entered , but MATLAB has not recognized it ???? and why ???!!
wt is used to specify that you're creating a text file.
see the help of fopen
and to write float numbers :
p = rand();
q = 3*p;
tf = pi ;
fid = fopen('myparams.py', 'wt');
fprintf(fid,'p1 =%10.3f\n',p);
fprintf(fid,'q =%10.3f\n',q);
fprintf(fid,'tf=%10.3f\n',tf);
fclose(fid)
%10.3f means 3 numbers after the comma and a length of 10 characters between the = sign and the last number written (third decimal in this case).
farzad
farzad el 14 de Oct. de 2014
I tried with also %10.3 , still only doesn't know tf , actually I have more parameters , let's put them all here , it's making me CRAZY !!
when I deactivate the tf line , the error jumps to Et : as follows :
p1 = 12.5; q=2*p1; tf=1.2; tb=7.49; tw=1.63; teta=85; d0=70;
dep=1;
%default values
Et= 109.36e9; nut=0.3 Eb= 290.482e9; nub=0.063; MVar=1; MakeVar(MVar);
and in the MakeVar file(function) :
function MakeVar(MVar)
fid = fopen('myparams.py', 'w');
fprintf(fid,'p1 = %0.12f\n',p1);
fprintf(fid,'q = %0.12f\n',q); fprintf(fid,'tf = %10.3f\n',tf); fprintf(fid,'tb = %10.3f\n',tb); fprintf(fid,'tw = %d\n',tw); fprintf(fid,'teta = %d\n',teta); fprintf(fid,'d0 = %0.12f\n',d0);
fprintf(fid,'saveData = %d\n',saveData); fprintf(fid,'saveFig = %d\n',saveFig); fprintf(fid,'saveMov = %d\n',saveMov);
fprintf(fid,'dep = %1.8f\n',dep); fprintf(fid,'Et = %1.8f\n',Et); fprintf(fid,'nut = %d\n',nut); fprintf(fid,'Eb = %1.8f\n',Eb); fprintf(fid,'nub = %d\n',nub);
fclose(fid);
farzad
farzad el 14 de Oct. de 2014
Orion , for your question in the other response , I have put my lines here
if I have not defined the function variables complete , why does it work for p and q and not of tf , and then for the next lines it works , but stops again at Et ??
maybe the problem is how my parameters formats are , and how I try to use those %d or %12f ? maybe I am doing something wrong there ?
for what you wrote, you should get an error with the line
fprintf(fid,'p1 = %0.12f\n',p1);
because p1 and all other parameters are note defined in the function MakeVar.
you have two options : 1) add all your parameters as input arguments : MakeVar(MVar,p1,q,tf,...,nub).
2) don't use a separated function, copy the code of MakeVar.m after the definition of your parameters :
p1 = 12.5;
q=2*p1;
tf=1.2;
tb=7.49;
tw=1.63;
teta=85;
d0=70;
dep=1;
Et= 109.36e9;
nut=0.3;
Eb= 290.482e9;
nub=0.063;
MVar=1;
fid = fopen('myparams.py', 'w');
fprintf(fid,'p1 = %0.12f\n',p1);
fprintf(fid,'q = %0.12f\n',q);
fprintf(fid,'tf = %10.3f\n',tf);
fprintf(fid,'tb = %10.3f\n',tb);
fprintf(fid,'tw = %d\n',tw);
fprintf(fid,'teta = %d\n',teta);
fprintf(fid,'d0 = %0.12f\n',d0);
% fprintf(fid,'saveData = %d\n',saveData); % saveDatanot defined
% fprintf(fid,'saveFig = %d\n',saveFig); % saveFig);not defined
% fprintf(fid,'saveMov = %d\n',saveMov); % saveMovnot defined
fprintf(fid,'dep = %1.8f\n',dep);
fprintf(fid,'Et = %1.8f\n',Et);
fprintf(fid,'nut = %d\n',nut);
fprintf(fid,'Eb = %1.8f\n',Eb);
fprintf(fid,'nub = %d\n',nub);
fclose(fid);
farzad
farzad el 14 de Oct. de 2014
Thank you so much
well I just did not want to mix them All in one file , So probably I will have to
using option 1 , I just added one more parameter to the function input and it gave me error : too many input arguments
if you're keeping a separated function, and add new arguments.
function MakeVar(MVar,p1,q,tf,...,nub)
don't forget to change you're calling syntax in the first mfile otherwise their will be not enough input arguments.
farzad
farzad el 14 de Oct. de 2014
shall I ask how is it different if I define a function like just :
makeVar(mVar)
or I write :
[a,b]=makeVar(f,g,h,..)
and what is the difference if we write
Values = makeVar(f,g,h,...) ?
farzad
farzad el 14 de Oct. de 2014
by the way it works when I put the all together , but I wanted to see why doesn't it work as the option one
if you create a mfile as a function :
function [y1,..,ym] = func(x1,..,xn)
then when you call this function you haveto define the n input argument, and not necessary the n output.
you can call it as :
func(x1,..xn) % no output
y1 = func(x1,..xn) % one output
[y1,..ym] = func(x1,..xn) % m outputs
the number of input arguments must be the same in the definition of you function and in the calling syntax (actually, there are some advanced programming techniques using nargin and varargin, which allows you to have a varible number of input arguments, but it's not the topic here)
farzad
farzad el 14 de Oct. de 2014
thank you very much Orion
farzad
farzad el 14 de Oct. de 2014
and by the way ,what is a good dictionary for all the formats like %d , %12f and so ?

Iniciar sesión para comentar.

Más respuestas (1)

farzad
farzad el 14 de Oct. de 2014
After Running and getting the Error , I type tf and I get a result , exactly what I have entered , but MATLAB has not recognized it ???? and why ???!!

1 comentario

Orion
Orion el 14 de Oct. de 2014
you're using serparated m-file.
are they functions or scripts ?
if they are functions, are all your variables defined (or passed as arguments) in the second mfile ?

Iniciar sesión para comentar.

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 14 de Oct. de 2014

Comentada:

el 14 de Oct. de 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by