- 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 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
per isakson
on 6 Nov 2014
Edited: per isakson
on 7 Dec 2014
"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 Comments
per isakson
on 10 Nov 2014
Edited: per isakson
on 10 Nov 2014
"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.
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!