i want to create a function that contains table and produces an output

조회 수: 1 (최근 30일)
Raza
Raza 2014년 10월 1일
편집: Stephen23 2014년 10월 1일
i want to create a function that contains table and produces an output after comparing the variable name with the value present in the table
example
function name is compare
compare(a)
it produces 23
as it will be defined in function that
a=23
b=24 and so on

채택된 답변

Stephen23
Stephen23 2014년 10월 1일
편집: Stephen23 2014년 10월 1일
Does it need to be a function ? Does it really need to contain a table ?
Assuming that the "name" is a string (if it isn't then you can use inputname ), then there are several ways you could achieve this:
  • given then names in your example 'a' and 'b', this will work for individual characters from 'a' to 'z':
name_string - 'a' + 23
this can easily be defined as a function:
>>fun = @(s) s-'a'+23;
>>fun('b')
ans = 24
  • for more complicated name strings you could use a struct to achieve this functionality:
>>A = struct('aa',23,'bb',24);
>>A.('bb')
ans = 24
You might also like to read about double and strcmp .

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by