Why is "undefined" a variable that I have previously defined???

5 Ansichten (letzte 30 Tage)
Adrien
Adrien am 5 Jul. 2013
Kommentiert: John D'Errico am 21 Feb. 2018
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'

Antworten (3)

Matt J
Matt J am 5 Jul. 2013
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 Kommentare
Adrien
Adrien am 5 Jul. 2013
Bearbeitet: Adrien am 5 Jul. 2013
What is strange is that if I use 'd' in an other function (let's say H) then H works without any problem. I have checked and 'd' is not cleared anywhere. Th error message is :
??? Input argument "d" is undefined.
Here is part of the code :
function [ ] = F(NM, [...], d)
close all
tic
[several sub-functions that work correctly]
figure(4)
z=bidon(d) [This works correctly]
W=G(p, Nk, [...], d); [This does not work]
[correctly working end of the function]
end
Matt J
Matt J am 5 Jul. 2013
Bearbeitet: Matt J am 5 Jul. 2013
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.

Melden Sie sich an, um zu kommentieren.


Guru
Guru am 5 Jul. 2013
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 Kommentare
Danielle Klawitter
Danielle Klawitter am 21 Feb. 2018
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
John D'Errico am 21 Feb. 2018
@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.

Melden Sie sich an, um zu kommentieren.


Adrien
Adrien am 5 Jul. 2013
Finally it's OK, I had miscorrected the inputs of G in F, the problem seems to be solved. Thank you all :) !

Kategorien

Mehr zu Function Creation finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by