- copied your code to two m-files. (See below)
- run the script, MakeVarCall
- got the error: Undefined function or variable 'fid'. (See below.) The reason to the error is that no file is being opened (and there is no variable named  fid  ).
writing some parameters from mfile on txt file and problem with some parameters
1 回表示 (過去 30 日間)
古いコメントを表示
Hi all
I am trying to write some parameters from one matlab file , by defining a function that does this writing on a txt file
on the main file I have some parameters like :
p1 = 12.5;
q=2*p1;
tb=7.49;
tf=1.2;
tw=1.63;
teta=85;
d0=70;
dep=1;
Et= 109.36e9;
nut=0.3;
Eb= 290.482e9;
nub=0.063;
then I call the function as :
MakeVar(tf,Et,nut,Eb,nub)
then , in the function m-file I define it like this :
function MakeVar(tf,Et,nut,Eb,nub)
fprintf(fid,'p1 = %0.12f\n',p1);
fprintf(fid,'q = %0.12f\n',q);
fprintf(fid,'tf = %0.12f\n',tf);
fprintf(fid,'tb = %0.12f\n',tb);
fprintf(fid,'tw = %d\n',tw);
fprintf(fid,'teta = %d\n',teta);
fprintf(fid,'d0 = %0.12f\n',d0);
...
The rest doesn't matter , just the same for all the parameters
I would like to ask you to try it , on your matlab
my question is , before , I was not putting any parameters in the parenthesis of makevar()but Matlab was giving me error
the strange thing was that it was not sensitive on p1 , but when it wanted to write tf , giving me error that tf is not defined , so I had to include it in the function parenthesis , I don't know it's output in this figure or input ?? cause the main shape of the function is :
function [y1,..,ym] = func(x1,..,xn)
and mine doesnt look like this one , So I need to do it without these strangely setting some of the parameters and not setting some , because I might need to change the names or number of them , so I don't want to check it every time
thank you so much
0 件のコメント
採用された回答
per isakson
2014 年 11 月 6 日
編集済み: per isakson
2014 年 12 月 7 日
"before"
>> MakeVar()
Undefined function or variable 'fid'.
Error in MakeVar (line 3)
fprintf(fid,'p1 = %0.12f\n',p1);
"then I call the function as"
>> MakeVar(tf,Et,nut,Eb,nub)
Undefined function or variable 'fid'.
Error in MakeVar (line 3)
fprintf(fid,'p1 = %0.12f\n',p1);
in both cases MATLAB throws an error for the first undefined variable, which it encounters, i.e. fid. It never checks whether p1 exists. That's not "strange" !   (You don't tell what error message your system shows.)
 
I don't exactly understand what you mean by the last part of your question. However, one cannot argue with MATLAB. One has to play according to its rules, which are defined in the documentation.
 
In response to comment:
I have made a new experiment with the code, which you show in your question.
My result differs from yours and my conclusion is that you run a code, which is different compared to the code you show in your question.
I think that the reason to your problem is a lack of understanding of the scope of variables. The variables p1 and q are not passed to the workspace of the function, MakeVar. See Base and Function Workspaces.
Run MakeVarCall
>> MakeVarCall
Undefined function or variable 'fid'.
Error in MakeVar (line 2)
fprintf(fid,'p1 = %0.12f\n',p1);
Error in MakeVarCall (line 14)
MakeVar(tf,Et,nut,Eb,nub)
>>
where   MakeVarCall.m   contains the following code
p1 = 12.5;
q=2*p1;
tb=7.49;
tf=1.2;
tw=1.63;
teta=85;
d0=70;
dep=1;
Et= 109.36e9;
nut=0.3;
Eb= 290.482e9;
nub=0.063;
MakeVar(tf,Et,nut,Eb,nub)
where   MakeVar.m   contains the following code
function MakeVar(tf,Et,nut,Eb,nub)
fprintf(fid,'p1 = %0.12f\n',p1);
fprintf(fid,'q = %0.12f\n',q);
fprintf(fid,'tf = %0.12f\n',tf);
fprintf(fid,'tb = %0.12f\n',tb);
fprintf(fid,'tw = %d\n',tw);
fprintf(fid,'teta = %d\n',teta);
fprintf(fid,'d0 = %0.12f\n',d0);
end
5 件のコメント
per isakson
2014 年 11 月 10 日
編集済み: per isakson
2014 年 11 月 10 日
"why the first undefined parameter for you is p1"   No it's not. It's   fid.   I assume that you run a code, which is different compared to the code you show in your question.
See addition to my answer.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!