A very strange problem
이전 댓글 표시
Hi friends!
After spending an hour on a seemingly simple problem I was not able to figour out what is wrong and this is why
I am bothering you, Consider the following commands:
D0=1;D1=1;
f='@(dt)[(-D0)*dt^0.5+(-D0*D1)*dt^1.5/2;1/2*(-1+(1)+(2*D1+2*D0^2)*dt/2)]';
f=vectorize(f);f=str2func(f);f(1)
which gives me an error saying that "Unrecognized function or variable 'D0'.". But, when I copy the second line and run it, it works!!! To get what I mean the following is what happend on my command window exactly (see also a printscreen of my command window attached)
>> D0=1;D1=1;
f=@(dt)[(-D0).*dt.^0.5+(-D0.*D1).*dt.^1.5./2;1./2.*(-1+(1)+(2.*D1+2.*D0.^2).*dt./2)];
f=vectorize(f);
f=str2func(f);
f(1)
Unrecognized function or variable 'D0'.
Error in code2>@(dt)[(-D0).*dt.^0.5+(-D0.*D1).*dt.^1.5./2;1./2.*(-1+(1)+(2.*D1+2.*D0.^2).*dt./2)]
>> f=@(dt)[(-D0).*dt.^0.5+(-D0.*D1).*dt.^1.5./2;1./2.*(-1+(1)+(2.*D1+2.*D0.^2).*dt./2)];
>> f(1)
ans =
-1.5
1
I find this really crazy and am impatiently looking forward to hear from you.
Thanks in advance!
Babak
댓글 수: 4
Stephen23
2022년 1월 14일
"A very strange problem"
It is not a strange problem at all.
The STR2FUNC documentation clearly states in the second sentence of its description that "Function handles created using str2func do not have access to variables outside of their local workspace or to nested functions" and warns that an error will be thrown if you try to access such variables.
Reading the documentation is the best way to know what a function does.
Mohammad Shojaei Arani
2022년 1월 14일
"why matlab is like this?"
Computers cannot read your mind (yet), they have to follow rules. Those rules include where variables/functions/whatever are visible (scoping) to other functions or operators. Although in theory a computer could look in every existing workspace for a variable, there are several major problems with this approach:
- workspaces change, meaning that different variables come in and out of scope, leading to unpredictable code which is almost impossible to debug.
- it would be slooooooooooow.
"I have spent some weeks and prepared extremely long function handles which are saved as text files."
Function handles store information about their scope when they are created: you can save function handles in .mat files. Converting to text or storing functions as text does not store any of this information, so your design is fundamentally lossy.
Steven Lord
2022년 1월 14일
If your functions are that long, change your text files to MATLAB function files and use function handles to those files. This could also allow you to extract out common code segments into helper functions that can be called by one or more of your large function files.
채택된 답변
추가 답변 (2개)
f='@(D0,D1,dt)[(-D0)*dt^0.5+(-D0*D1)*dt^1.5/2;1/2*(-1+(1)+(2*D1+2*D0^2)*dt/2)]';
f=vectorize(f);
f=str2func(f);
D0=1;D1=1;dt=1 ;
f(D0,D1,dt) % you need to input three variables to the function
댓글 수: 3
Mohammad Shojaei Arani
2022년 1월 14일
Yes, but that can be achieved with a very small modification.
f='@(D0,D1,dt)[(-D0)*dt^0.5+(-D0*D1)*dt^1.5/2;1/2*(-1+(1)+(2*D1+2*D0^2)*dt/2)]';
f=vectorize(f);
f=str2func(f);
D0=1;D1=1;;
f=@(dt)f(D0,D1,dt);
f(1)
Mohammad Shojaei Arani
2022년 1월 15일
An alternative solution is to download afslim() from,
D0=1;D1=1;
f='@(dt)[(-D0)*dt^0.5+(-D0*D1)*dt^1.5/2;1/2*(-1+(1)+(2*D1+2*D0^2)*dt/2)]';
f=afslim(vectorize(f),D0,D1)
f(1)
카테고리
도움말 센터 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!