Global variable scope is not applied in try and catch

global Name;
clear all;
close all force;
prompt = 'Enter Name';
%Name = input(prompt,'s');
Name = input(prompt);
for i=1:10
Depth;
DetectFaceandEyes;
calcEyedistance
try
load db;
euclideanDistance = round(euclideanDistance,1);
F=[euclideanDistance Name distanceAsString];
db=[db;F];
save db.mat db
catch
db =[euclideanDistance Name distanceAsString];
save db.mat db
end
end
Undefined function or variable 'Name'.
Error in Trianing (line 18) db =[euclideanDistance Name distanceAsString];

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 7 de Mayo de 2016
Editada: Azzi Abdelmalek el 7 de Mayo de 2016
global Name;
clear all;
close all force;
prompt = 'Enter Name';
Name = input(prompt)
You declare Name as global, then you clear all the variables, finally you ask to enter the variable Name. You have to remove the: clear all

6 comentarios

But even after i declare global Name in another script it is showing error
You have not read this and you really really need to.
Thank You Image Analyst,I studied this guideline and i will take care in future,I follow these guideline i will be more specific in title, full error detail
Image Analyst
Image Analyst el 7 de Mayo de 2016
Editada: Image Analyst el 7 de Mayo de 2016
So, you've accepted the answer but last thing you said was there it's still showing an error even after you removed the call to clear.
So, which is it? Is it solved or not?
Ankit Singh
Ankit Singh el 7 de Mayo de 2016
Editada: Ankit Singh el 7 de Mayo de 2016
yes it solved
If it wasn't removing the clear, then what was the solution?

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 7 de Mayo de 2016
Editada: Image Analyst el 7 de Mayo de 2016
You have
global Name;
clear all;
Now, the help says "A variable that is global, then clear removes it from the current workspace, but it remains in the global workspace". Now I think that's a little ambiguous - is Name there or not? When you recreate Name with a call to input():
Name = input(prompt);
Is that a new local variable called Name, or is that the same Name in the global workspace again? I'm not sure. My guess is that it's a local variable that unfortunately now has the same name as the variable in the global workspace. But they're two different variables now. The best thing to do is (like Azzi said) to remove the call to clear.

Categorías

Más información sobre Data Type Identification en Centro de ayuda y File Exchange.

Preguntada:

el 7 de Mayo de 2016

Comentada:

el 7 de Mayo de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by