Question

I have made many functions where I do not always want to require input, so the first lines in many of them look like:

function something = thisIsMyFunction(OptionalStruct)  
   if(exist('OptionalStruct') 
      building on existing struct;  
   else
      build a new struct; 
   end
end

According to the docs, the exist() search goes much faster when I can pass in a type along with the option, so it will only search for that type. I want to know if in this case (or ever) a struct is a variable and I can say:

if(exist('OptionalStruct', 'var')
Was it helpful?

Solution

You could use use isstruct to check if the variable is a structure.

if(exist('OptionalStruct') 
    if(isstruct(OptionalStruct))
        building on existing struct;  
else
  build a new struct; 
end

OTHER TIPS

Whatever is assigned in a statement like

varName = ...;

is a variable and will make exist('varName', 'var') true.

It is totally irrelevant what type of value that variables holds or refers to.

And, seconding natan's comment: It should take less time to test a thing like than this, than to post this question on SO.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top