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

1 Ansicht (letzte 30 Tage)
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 Kommentare
Cedric
Cedric am 14 Sep. 2017
Bearbeitet: Cedric am 14 Sep. 2017
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 am 14 Sep. 2017
Bearbeitet: Stephen23 am 14 Sep. 2017
" 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Cam Salzberger
Cam Salzberger am 14 Sep. 2017
Bearbeitet: Cam Salzberger am 14 Sep. 2017
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 Kommentar
Konstantinos Tsitsilonis
Konstantinos Tsitsilonis am 14 Sep. 2017
Bearbeitet: Konstantinos Tsitsilonis am 14 Sep. 2017
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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by