フィルターのクリア

Error that says that the some variable is nonexistent?

3 ビュー (過去 30 日間)
S
S 2014 年 11 月 12 日
コメント済み: the cyclist 2014 年 11 月 12 日
I am trying to solve this problem:
You are testing a functional near infrared (fNIR) device and measuring how well each subject is able to control the computer mouse using it. Write a function getcogdata.m that returns the requested measurement for the requested user. If the requested user does not exist, return an empty matrix. If no user is requested, return a cell array of values for all users.
id name age height score
33 Joe 27 5.9 9.5
10 Sally 25 6.1 9.3
48 Harry 23 5.8 9.7
41 Ann 19 6.0 9.4
For example:
>> getcogdata('age',33)
ans =
27
>> getcogdata('age')
ans =
[27] [25] [23] [19]
>> getcogdata('name',10)
ans =
Sally
>> getcogdata('name')
ans =
'Joe' 'Sally' 'Harry' 'Ann'
The code I have so far is:
function [ output_args ] = getcogdata(info, value )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
p = struct('id', {'33' '10' '48' '41'}, ...
'name', {'Joe' 'Sally' 'Harry' 'Ann'}, ...
'age', {27 25 23 19}, ...
'height', {5.9 6.1 5.8 6.0}, ...
'score', {9.5 9.3 9.7 9.4});
elementsMatchingGroup = value == [p.value];
switch lower(info)
case 'id'
output_args = {p(elementsMatchingGroup).id};
case 'name'
output_args = {p(elementsMatchingGroup).name};
case 'age'
output_args = {p(elementsMatchingGroup).age};
case 'height'
output_args = {p(elementsMatchingGroup).height};
case 'score'
output_args = {p(elementsMatchingGroup).score};
end
But for some reason I keep getting an error in this line saying "Reference to non-existent field 'value'." :
elementsMatchingGroup = value == [p.value];
What am I doing wrong??

採用された回答

the cyclist
the cyclist 2014 年 11 月 12 日
I think you want
elementsMatchingGroup = value == [p.id]
rather than p.value
  2 件のコメント
S
S 2014 年 11 月 12 日
yes I tried that too but whenever I type go to the command window to test it out it comes as null set. For example I put in:
>> getcogdata('age',33)
ans =
{}
the cyclist
the cyclist 2014 年 11 月 12 日
That's because you defined the id variable as a set of strings, but you are using an input that is numeric. You need to make them consistent.
I suggest making id numeric:
p = struct('id', {33 10 48 41}, ...
With the other change I suggested,
getcogdata('age',33)
returns
[27]

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by