Convert cellarray with variables to cellarray of the variable names as strings

조회 수: 2 (최근 30일)
Hi all,
I have a cell array as shown below, with different variables named as follows:
VarCell = {P_scav, T_exh, P_eng ... etc} ;
Is there a way to convert VarCell such that
VarCellChar = {'P_scav', 'T_exh', 'P_eng' ... etc} ;
In words, is there a way to convert VarCell's variable names in to a cell array with those variable names as strings?
Thanks in advance for your help,
KMT.
  댓글 수: 2
Cedric
Cedric 2017년 9월 14일
편집: Cedric 2017년 9월 14일
In 20+ years of using MATLAB, I have rarely seen a case where working with variable names as strings was appropriate. What do you want to achieve ultimately? Mabye there is a better way.
Stephen23
Stephen23 2017년 9월 14일
편집: Stephen23 2017년 9월 14일
" is there a way to convert VarCell's variable names in to a cell array with those variable names as strings?"
Yes there are ways, but it is highly unlikely to be a good way to write code and solve your task:
What is the actual task that you are trying to achieve? I suspect your real task could be solved in a much better way, rather than relying on slow and buggy code that you are asking about:
For as start, it seems that you have put meta-data into your variable names. This is a bad design decision, because then accessing that meta-data can only be done using slow and complex methods. Much better is to store meta-data inside an array, as data in its own right. Then your task would also be much much simpler.

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

채택된 답변

Cam Salzberger
Cam Salzberger 2017년 9월 14일
편집: Cam Salzberger 2017년 9월 14일
Hello Konstantinos,
There kind of is, but Cedric is absolutely right. I have very rarely seen variable names as strings to be necessary in a workflow. However, if you'd like to do it, you can define your own function that then calls inputname:
function varNames = getVarNames(varargin)
varNames = cell(1,nargin);
for k = 1:nargin
varNames{k} = inputname(k);
end
end
This issue with this is the way that you are looking to call it. You say that you first create your cell array:
VarCell = {P_scav, T_exh, P_eng ... etc} ;
At this point, VarCell does not contain any information about which variables were used to create it. It only knows what the data in those variables was, since it now contains a copy of that data. You'll need to call this getVarNames function with each of those input arguments.
VarCellChar = getVarNames(P_scav, T_exh, P_eng ... etc);
at which point you might as well just create the cell array manually. What you could do is make this function output both VarCell and VarCellChar, and just use it as a packaging and naming function though:
function [varCell, varNames] = getVarCellAndNames(varargin)
varCell = varargin;
varNames = cell(1,nargin);
for k = 1:nargin
varNames{k} = inputname(k);
end
end
It'll need to be your own function, since inputname only works on function inputs. And it requires the loop, since inputname only takes scalars.
But definitely think hard about if there is another better way to go about getting the result you want.
-Cam
  댓글 수: 1
Konstantinos Tsitsilonis
Konstantinos Tsitsilonis 2017년 9월 14일
편집: Konstantinos Tsitsilonis 2017년 9월 14일
Thank you for your answer. The reason I want those variable names returned as strings is because I want to use them as labels in plots that I am generating within a for loop.
There is really no practical purpose to that, but as I tend to have more and more variables, it tends to be quite laborious to re-define them by hand, especially as they tend to change from case to case.
Anyway, thanks again for your assistance!

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

추가 답변 (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