我正在实现以下函数来查询所有名称为"Texas"的大学条目:
function [] = queryHandler(structName, fieldName, fieldValue)
% query for college names
for i = 1:1:length(structName.fieldName)
if strcmp(structName(i).fieldName, fieldValue)
if strcmp(fieldName, 'name')
foundNames(i) = structName(i);
elseif strcmp(fieldName, 'nickName')
foundnickNames(i) = structName(i);
elseif strcmp(fieldName, 'location')
foundLoactions(i) = structName(i);
end
end
if strcmp(fieldName, 'name')
foundNames
elseif strcmp(fieldName, 'nickName')
foundnickNames
elseif strcmp(fieldName, 'location')
foundLocations
end
end
这是主脚本:
%collegeDatabase main test script
clc
clear
%preallocation for speed
names = {'Kansas', 'KSU', 'Oklahoma', 'Texas', 'Alabama', 'LSU', 'Mizzou', 'Stanford', 'Auburn', 'Oregon'};
nickNames = {'Jayhawks', 'Wildcats', 'Sooners', 'Longhorns', 'Crimson Tide', 'Tigers', 'Tigers', 'Cardinals', 'Tigers', 'Ducks'};
locations = {'Kansas', 'Kansas', 'Oklahoma', 'Texas', 'Alabama', 'Louisiana', 'Missouri', 'California', 'Alabama', 'Oregon'};
college(10).name = {' '};
college(10).nickName = {' '};
college(10).location = {' '};
%creation of structure for college names, nicknames, loactions
for i = 1:length(names)
college(i).name = names(i);
college(i).nickName = nickNames(i);
college(i).loaction = locations(i);
end
queryHandler(college, name, Texas)
我正试图获得这些输入来搜索大学(I)。数据库中的名字是"德克萨斯州"。我必须如何处理此处的输入才能解决此问题?感谢
首先,在主脚本中,您可能应该调用queryHandler(college, 'name', 'Texas')
。
其次,在您的函数中,您应该将所有structName(i).fieldName
调用更改为structName(i).(fieldName)
。
最后,在for循环中,您应该转到structName(0).(fieldName)
。结构本身没有元素fieldName
,但每个单独的元素都有。