Borrar filtros
Borrar filtros

Check for missing object in workspace

11 visualizaciones (últimos 30 días)
Stephen
Stephen el 11 de Ag. de 2011
In a script, how do I check to see whether an object (array) exists before I try to operate on it. Something like:
if object exists then

Respuestas (2)

Image Analyst
Image Analyst el 11 de Ag. de 2011
From the help for "exist()":
The following example indicates that testresults is both a variable in the workspace and a folder on the search path:
exist('testresults','var')
ans =
1
exist('testresults','dir')
ans =
7

Stephen
Stephen el 11 de Ag. de 2011
Thanks, but the exists function returns an error if the object does not exist. The help documentation says it should return a zero, so I assumme this is a bug.
Is there another test? I don't want to use a try-catch because the selected recovery algorithm may depend on whether the error is due to object==null or some other type of error.
  2 comentarios
Stephen
Stephen el 11 de Ag. de 2011
Oops, my mistake, there is no bug. I was using exist(variable_name) instead of exist('variable_name').
Can this string be deleted?
Image Analyst
Image Analyst el 11 de Ag. de 2011
I was just going to say that. That's a very common mistake. No you can't delete the string - otherwise exist() would have no arguments whatsoever that you're passing in. You have to pass in something.

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by