Matlab函数isfield简介
函数功能: 判断输入是否是
结构体
数组的域(成员)。
调用格式:
tf = isfield(S, 'fieldname')
检查
结构体S是否包含由fieldname指定的域, 如果包含, 返回逻辑1; 如果S不包含fieldname域或者S不是
结构体类型的, 返回逻辑0。
tf = isfield(S, C)
其中C是一个包含多个字符串的
元胞数组,isfield判定由这些字符串表示的域是否是
结构体的域。返回值是个逻辑型数组。
程序示例
close all; clear; clc;
student =
struct('name', 'John', 'age', 20, 'score', 90);
fprintf('Is ''name'' a field of student
structure? %dn',isfield(student, 'name'));
fprintf('Is ''salary'' a field of student
structure? %dn',isfield(student, 'salary'));
isfield(student, {'name', 'salary', 'score'})
输出结果:
Is 'name' a field of student
structure? 1
Is 'salary' a field of student
structure? 0
ans =
1
0
1