Matlab: Using the exist function within a structure

In summary, the exist function in Matlab is used to check for the existence of variables, files, and functions in the current workspace. It can also be used within structures and nested structures. If there are variables or functions with the same name in different scopes, the exist function will return a value of 1 for the one in the current scope and 0 for the one in a different scope. Additionally, the exist function can also check for the existence of classes, packages, and built-in functions in Matlab.
  • #1
craigpmorgan
8
0
Hi All,

Does anyone if it's possibe to use the 'exist' function in Matlab to check whether any data has been entered into a particular field of a structure. I'd like something like the following, where 'identification' is a user-input variable, 'team' is the structure and 'city' is the field.

identifiaction = input('Enter I.D: ');
X = exist(team(identification).city);

if X == 1;
action

else
action

end

I need this to control where new data is input into the structure. Thanks for your time.

Craig
 
Physics news on Phys.org
  • #2
try this:

X = isfield(team,'city')
 

1. What is the purpose of the exist function in Matlab?

The exist function in Matlab is used to check whether a variable, file, or function exists in the current workspace. It returns a value of 1 if the item exists, and 0 if it does not.

2. How can the exist function be used within a structure?

The exist function can be used within a structure by passing the name of the variable or function as the first argument, and the string 'var' or 'func' as the second argument. This will check if the variable or function exists within the specified structure.

3. Can the exist function be used to check for nested structures?

Yes, the exist function can be used to check for nested structures. You can pass the name of the nested structure as the first argument, followed by the string 'struct' as the second argument. This will check if the nested structure exists within the specified structure.

4. How does the exist function handle variables or functions with the same name in different scopes?

If there are variables or functions with the same name in different scopes, the exist function will return a value of 1 for the one that is in the current scope, and a value of 0 for the one that is in a different scope.

5. Are there any other uses for the exist function?

Yes, besides checking for the existence of variables, files, and functions, the exist function can also be used to check for the existence of classes, packages, and built-in functions in Matlab.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
803
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
801
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top