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

1 回表示 (過去 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 Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by