How to check whether a structure exists?

Dear all:
I have a simple question and I can't find an answer on the web...
I have a function that takes arguments in. One argument is a structure array, and inside the function I want to test whether this argument is specified and feed into the function.
I tried isexist(), but this is not for a structure. isstruct() can test whether it is a structure, but the name must exist first. isfield() can check if a structural field exist, but not the structure itself.
So my question is, how to test whether a structure exist?
Any help is appreciated.
Cheers
Ben

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 7 Mai 2011

1 Stimme

exist('structurename','var')
[EDITED, Jan Simon 25-Feb-2012 22:00 UTC]: exists -> exist

6 Kommentare

Ben Wang
Ben Wang am 7 Mai 2011
Hi Walter:
It doesn't work for me, I have the following message:
>> exists('param_fix','var')
??? Undefined function or method 'exists' for input arguments of type 'char'.
then I tried:
>> exists(param_fix,'var')
??? Undefined function or method 'exists' for input arguments of type 'struct'.
Maybe I miss something obvious here...
Cheers
Ben
Teja Muppirala
Teja Muppirala am 7 Mai 2011
I think Walter meant EXIST not EXISTS
if exist('param_fix','var') && isstruct(param_fix)
.
.
.
Walter Roberson
Walter Roberson am 7 Mai 2011
Yeh, Mathworks used the wrong command name :)
Roger Parkyn
Roger Parkyn am 2 Feb. 2017
If you want to also check whether specific fields exist go to this file on the File-Exchange: https://au.mathworks.com/matlabcentral/fileexchange/28142-recursively-check-fields-of-a-structure-exist
This does not work for me:
exist(nonExistantFileStruct, 'var')
??? Undefined function or method 'exist' for input arguments of type 'struct'.
Walter Roberson
Walter Roberson am 17 Okt. 2017
Pass in the name of the structure, not the structure itself.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Céldor
Céldor am 31 Mär. 2012

1 Stimme

This thread is old but if someone still needs an answer then
exist('var_name') works and returns 0 if a structure does not exist in memory
Regards

2 Kommentare

Jan
Jan am 31 Mär. 2012
This is no sufficient solution in opposite to the advice Walter gave 329 days ago: "exist('var_name')" replies a non-zero if there is a corresponding .m, .p, .mex file or folder in the path, or if there is a Java class with this name. Because all these chances are checked, "exist()" is slow when used without 2nd argument. See "help exist".
Walter Roberson
Walter Roberson am 31 Mär. 2012
But it can return non-zero if there happens to be something else by the same name, such as a function or directory. It is better to use the optional second argument to be specific that you are inquiring about a variable.

Melden Sie sich an, um zu kommentieren.

Matt Fig
Matt Fig am 7 Mai 2011

0 Stimmen

I am not entirely sure what you want to do, but here is an example input parsing... You don't have to make your function error as I do, but the checks are there.
function [] = takestwoargs(A,B)
% Takes two arguments, the first is a structure, the second
% is a double. Both are required.
if nargin~=2
error('Two input arguments required.')
elseif ~isstruct(A)
error('The first input must be a structure.')
elseif ~isa(B,'double')
error('The second argument must be a double.')
end
%
%
%
%
EDIT In response to your comment below. To make defaults, you could do something like this:
function [] = takestwoargs(A,B)
% Takes two arguments, the first is a structure, the second
% is a double. Both are required.
N = nargin; % Check how many inputs were passed.
if N==0
A = struct('string','hello','yes','lengths',4);
B = 5;% Defaults.
elseif N==1
B = 5;% Default.
else
% Or make defaults and issue a warning that
% argumenst beyond the second are ignored....
error('Two many input arguments.')
end
if ~isstruct(A)
% Or make a default and issue a warning...
error('The first input must be a structure')
elseif ~isa(B,'double')
% Same as above...
error('The second argument must be a double')
end

2 Kommentare

Ben Wang
Ben Wang am 7 Mai 2011
Hi Matt:
Thanks for helping out here. The function looks something like the following:
function [] = likelihood(A,B)
and B is a structure.
I guess I could use
if nargin~=2
error('Two input arguments required.')
to test whether B exists, but I don't want to put a restriction since if it is not parse to the function, I could use a default value specified inside the function.
Sorry I didn't make myself clear before. Is there a function I can test B exists in the workspace?
Cheers
Ben
Matt Fig
Matt Fig am 7 Mai 2011
See my edit above.

Melden Sie sich an, um zu kommentieren.

Jason Garcia
Jason Garcia am 25 Feb. 2012

0 Stimmen

Or alternatively, you could try:
x =
x: 1
y: 2
z: 3
>> any(strcmp(fieldnames(x),'d'))
ans =
0
>> any(strcmp(fieldnames(x),'y'))
ans =
1

2 Kommentare

Jan
Jan am 25 Feb. 2012
More efficient: isfield(x, 'y')
Jason Garcia
Jason Garcia am 26 Feb. 2012
Ha, ya. :)

Melden Sie sich an, um zu kommentieren.

Kategorien

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by