簡體   English   中英

如何解決此腳本中的“未定義的函數或變量”錯誤?

[英]How to fix “Undefined function or variable” error in this script?

我執行以下功能查詢所有大學的條目,其名稱為“得克薩斯”:

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)

我正在嘗試獲取這些輸入以搜索College(i)。數據庫中的名稱為'Texas'。 為了解決這個問題,我與這里的輸入有什么關系? 謝謝

首先,在主腳本中,您可能應該調用queryHandler(college, 'name', 'Texas')

其次,在函數中,應將所有structName(i).fieldName調用都更改為structName(i).(fieldName)

最后,在for循環中,您應該轉到structName(0).(fieldName) 您的結構本身沒有元素fieldName ,但每個元素都有。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM