I'm implementing the following function to query for all college entries whose name is '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
Here is the main script:
%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 am trying to get these inputs to search for college(i).name is 'Texas' in the database. What do I have to do with the inputs here to fix this? Thanks
First, in the main script you should probably be calling queryHandler(college, 'name', 'Texas')
.
Second, in your function, you should change all of your structName(i).fieldName
calls to structName(i).(fieldName)
.
Finally, in your for loop, you should be going up to structName(0).(fieldName)
. Your struct itself does not have an element fieldName
, but each individual element does.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.