Writing Data to text file gives error,unknown parameter

4 Ansichten (letzte 30 Tage)
farzad
farzad am 14 Okt. 2014
Kommentiert: farzad am 14 Okt. 2014
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

Akzeptierte Antwort

Florian
Florian am 14 Okt. 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 Kommentare
farzad
farzad am 14 Okt. 2014
thank you very much Orion
farzad
farzad am 14 Okt. 2014
and by the way ,what is a good dictionary for all the formats like %d , %12f and so ?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

farzad
farzad am 14 Okt. 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 Kommentar
Orion
Orion am 14 Okt. 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 ?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by