How do i make print the variable name for a certain variable

Suppose there's this array [a,b,c,d . . . ].I'm trying to write a program that takes the product of two variables a and b and if a*b is in the array then it will print the variable name. For example if a=3 b=2 and c=6 then myfunction(a,b) will return the string'c'

4 个评论

A starting point for doing this is:
find(array==a*b)
This returns the index where a*b exists, or else a null matrix. Then the question is how do you want use the result? E.g. to simply return the value it would be: array(find(array==a*b)) Again if the value does not exist then you would get a null matrix which your function could return.
Why do you want to do this?
How is c defined? e.g., where does it come from?
I'm trying to write a program to build a Cayley table, where i can input various named matrices and it will give me a cell array arranged appropriately.
"Suppose there's this array [a,b,c,d . . . ]"
There is no array [a,b,c,d...]. There may be an array that is the result of concatenating [a,b,c,d...] but at this point matlab does not know anymore that the array values came from a,b,c and d.
Your whole setup sounds like a bad idea. There is never a good reason for a function to return the name of a variable.

请先登录,再进行评论。

回答(2 个)

myw=who;
now all you need to do is cycle through the values of the variables and return the myw.name(ind)
If I understand you correctly you would like to print a the content of a variable.
a = 2;b = 3;c = a*b;disp(c)

1 个评论

Actually i want to be able to input 6 and for my program to output the string that's the variable name.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by