我正在实现以下功能来查询名为“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).name是数据库中的'Texas'。我该怎么处理这里的输入来解决这个问题?感谢
答案 0 :(得分:0)
首先,在主脚本中,您可能应该调用queryHandler(college, 'name', 'Texas')
。
其次,在您的功能中,您应该将所有structName(i).fieldName
来电更改为structName(i).(fieldName)
。
最后,在你的for循环中,你应该到structName(0).(fieldName)
。您的结构本身没有元素fieldName
,但每个元素都有。