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

 採用された回答

Florian
Florian 2014 年 10 月 14 日

1 投票

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 件のコメント

farzad
farzad 2014 年 10 月 14 日
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 2014 年 10 月 14 日
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 ???!!
Orion
Orion 2014 年 10 月 14 日
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 2014 年 10 月 14 日
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 2014 年 10 月 14 日
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 ?
Orion
Orion 2014 年 10 月 14 日
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 2014 年 10 月 14 日
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
Orion
Orion 2014 年 10 月 14 日
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 2014 年 10 月 14 日
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 2014 年 10 月 14 日
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
Orion
Orion 2014 年 10 月 14 日
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 2014 年 10 月 14 日
thank you very much Orion
farzad
farzad 2014 年 10 月 14 日
and by the way ,what is a good dictionary for all the formats like %d , %12f and so ?

サインインしてコメントする。

その他の回答 (1 件)

farzad
farzad 2014 年 10 月 14 日

0 投票

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 件のコメント

Orion
Orion 2014 年 10 月 14 日
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 ?

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

タグ

質問済み:

2014 年 10 月 14 日

コメント済み:

2014 年 10 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by