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
2014년 10월 14일
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
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
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.
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).
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);
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);
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.
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,...) ?
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)
thank you very much Orion
and by the way ,what is a good dictionary for all the formats like %d , %12f and so ?
추가 답변 (1개)
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 ???!!
댓글 수: 1
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 Exchange에서 Data Type Identification에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
