Error that says that the some variable is nonexistent?

조회 수: 2 (최근 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개)

Community Treasure Hunt

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

Start Hunting!

Translated by