Why is "undefined" a variable that I have previously defined???
2 ビュー (過去 30 日間)
古いコメントを表示
Hello, I use a variable 'd' in a function F, such that this variable 'd' is defined as an input of this function F. In F, 'd' is used only in a sub-function G, where acts as an input of G. However, if I ask for the value of 'd' in F, there is no problem to get it, while Matlab says me that 'd' is "undefined" when it tries to use it in G. Does someone knows why? Thank you in advance for your help.
Ad'
0 件のコメント
回答 (3 件)
Matt J
2013 年 7 月 5 日
You have either failed to pass d to G() or you have cleared d at some point within G(). We need to see error messages and code to say more.
2 件のコメント
Matt J
2013 年 7 月 5 日
編集済み: Matt J
2013 年 7 月 5 日
You need to show more. My guess is that you passed too few arguments to G() by mistake. For example, if d is defined as the final of 5 input arguments to G()
function output = G(arg1, arg2, arg3, arg4, d)
....
but you only passed 4 arguments to G() when computing W,
W=G(a,b,c d);
then G() will not be able to find d in its workspace. You could use NARGIN to investigate this.
Guru
2013 年 7 月 5 日
or future reference, putting a little MATLAB code down makes things a lot more clear than trying to explain it with words, especially if your English is not of similar level to the other readers who would like to help you with an answer.
I am guessing you have something that looks like this:
function F(d)
% So d is an input to function F.
% Some random pieces of code
disp(d) % works and displays d
y = G; % undefined error because d is not defined
function y = G(d)
The short answer is that subfunctions have different workspaces as they are functions themselves, just happen to live in the same file as another funciton. Every function/subfunction/private function in MATLAB have their own workspace (memory set aside just for their use). The "shared" workspaces in MATLAB is the global workspace (free to any that declares a link to the global workspace and nested workspace (limited with various rules one of which is the same file)
The typical way we communicated between workspaces is by passing inputs and asking for outputs. So to fix your problem, do this instead (using my above dummy functions
function F(d)
% So d is an input to function F.
% Some random pieces of code
disp(d) % works and displays d
y = G(d);
% Now works because we provide a value for the 'd' in G's workspace which is a
% copy of the 'd' in F's workspace. They will have the same value but are not
% linked, ie if 'd' changes value in G's workspace, the change will not go back
% to the 'd' in F's workspace.
function y = G(d)
2 件のコメント
Danielle Klawitter
2018 年 2 月 21 日
Really not necessary to comment on the questioner's level of English. Let's see you go to a different country and see how well you do.
John D'Errico
2018 年 2 月 21 日
@Danielle - Actually, what was really unnecessary was to resurrect a question that was now dead for 5 years, creating a zombie question just to comment on a random statement, in what was in reality a good overall attempt at an answer. In fact, Guru merely said that IF your knowledge of English is poor, then it helps to provide code to help explain what you need. This is not an insult at all, merely a statement about how to ask a question that will most likely receive a useful answer.
参考
カテゴリ
Help Center および File Exchange で Function Creation についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!