writing some parameters from mfile on txt file and problem with some parameters

1 view (last 30 days)
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

Accepted Answer

per isakson
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"&nbsp! &nbsp (You don't tell what error message your system shows.)
&nbsp
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.
&nbsp
In response to comment:
I have made a new experiment with the code, which you show in your question.
  • 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&nbsp fid &nbsp).
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 &nbsp MakeVarCall.m &nbsp 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 &nbsp MakeVar.m &nbsp 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
per isakson on 10 Nov 2014
Edited: per isakson on 10 Nov 2014
"why the first undefined parameter for you is p1" &nbsp No it's not. It's &nbsp fid. &nbsp I assume that you run a code, which is different compared to the code you show in your question.
See addition to my answer.
farzad
farzad on 11 Nov 2014
Thanks a lot Per , I will check it , I don't have so much internet , I will check precisely and will put again my code ,but I think that's all , and the question is that why the other variables do not have that problem , while their condition is the same

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!