Re-read the variable name

조회 수: 2 (최근 30일)
Prakhar Modi
Prakhar Modi 2019년 8월 16일
답변: Walter Roberson 2019년 8월 16일
Hello Everyone,
I have alloted 5 variable names to 5 different 15x1 matrix, for example.
ec=data(:,1);
fe=data(:,2); and so on for Co3, Ca and Na
then I am merging all the data into variable x for some calculation so we get x=15x5 matrix. Now after the calculation I get an answer which is one of 5 different 15x1 matrix. Then how to get the variable name as an answer. Like the code should tell that the answer is either ec, fe, Co3, Na or Ca. Means the code should read the variable name which I have assigned in the very starting of the code.
Thanks in advance.
  댓글 수: 3
Prakhar Modi
Prakhar Modi 2019년 8월 16일
Thanks for the reply. I am already usign the indexing to know the column which is the answer. But then how to get the name. Because I have mentioned 5 variable but in actual I have 17 variable.
Walter Roberson
Walter Roberson 2019년 8월 16일
Stephen's V variable can easily be extended to hold 17 names

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2019년 8월 16일
In https://www.mathworks.com/matlabcentral/answers/475899-how-to-properly-convert-variable-names-into-a-chain-of-strings-in-matlab
Bruno and I show how you can determine the name of a variable that you pass to a function.
So what you can do is put your logic into a function, and pass the variables into the function to be operated on. Then when you have calculated the index of the variable that was used, you can pass the index value to inputname() to find out what the variable name was.
Coding in this manner permits you to only write the names of the variables as references to their values, instead of doing that and creating a cell array of character vectors (or a string array) that has the variable names as quoted strings the way that Stephen shows.
This approach is not recommended; what is recommended is to use a table of names the way that Stephen shows. One advantage of using a table of names is that if you do so you are not restricted to the format of MATLAB variable names. You could for example use table entries such as 'CO2 nmol/s'

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by