Convert cellarray with variables to cellarray of the variable names as strings
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Konstantinos Tsitsilonis
am 14 Sep. 2017
Bearbeitet: Konstantinos Tsitsilonis
am 14 Sep. 2017
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
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.
Akzeptierte Antwort
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
Weitere Antworten (0)
Siehe auch
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!