MATLAB: Error that says that the some variable is nonexistent

caseerrorgetcogdataswitch

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??

Best Answer

I think you want
elementsMatchingGroup = value == [p.id]
rather than p.value