use eval to define a variable from table and a name stored as cell

5 vues (au cours des 30 derniers jours)
ST
ST le 16 Juin 2022
Réponse apportée : ST le 17 Juin 2022
The cell data{} has 9 tables and i created another cell which is called c_data which contains variable names from the tables (data9 ,.... data1). In each tabel 160 variables are storred, and i need to fetch anoly those which are in c_data.
data = {dataD9 dataD8 dataD7 dataD6 dataD5 dataD4 dataD3 dataD2 dataD1};
c_data = {'IspFuel_Corr'; 'IspCOEo_Corr'; 'IspNOEo_Corr'; 'IspNOXEo_Corr'; 'IspTHCEo_Corr'; 'ArbInd1'; 'EffEngComb'; 'AI50_1'; 'MfIna'; 'LamdBrettEo'; 'PExhVes_1'; 'PExhVes_2'; 'PAirVes_1'; 'PAirVes_1'; 'Ssa_Esa'};
i use eval to create a variable z = data{:,i}.eval(c_data{k,1});
- with data{:,i} i loop all 9 tables which are in the data cell
- with eval(c_data{k,1}) i want to add the name to the variable to data{:,1}
in other words what i woudl like to have is : z = data{:,1}.IspFuel_Corr; ... first loop
z = data{:,1}.IspCOEo_Corr; ... second loop
.....
z= data{:,1}.Ssa_Esa; ... 15th loop or last loop for k, and then the same it should be done with data{:,2}. =>>> z = data{:,2}.IspFuel_Corr;
z = data{:,2}.IspCOEo_Corr; and so on until loop i is done.
the code and the error below:
data = {dataD9 dataD8 dataD7 dataD6 dataD5 dataD4 dataD3 dataD2 dataD1};
c_data = {'IspFuel_Corr'; 'IspCOEo_Corr'; 'IspNOEo_Corr'; 'IspNOXEo_Corr'; 'IspTHCEo_Corr'; 'ArbInd1'; 'EffEngComb'; 'AI50_1'; 'MfIna'; 'LamdBrettEo'; 'PExhVes_1'; 'PExhVes_2'; 'PAirVes_1'; 'PAirVes_1'; 'Ssa_Esa'};
for k = 1: length (c_data)
for i= 1:length(data)
% nexttile(t)
% savename = ['IspFuel_Corr'];
y = data{:,i}.Svt_ActAng_A; %iVVT
x = data{:,i}.Sve_ActAng_A; %eVVT
z= data{:,i}.eval(c_data{k,1});
end
end
Error using tabular/dotParenReference (line 69)
Unrecognized table variable name 'eval'.
Error in VVT_plots (line 38)
z= data{:,i}.eval(c_data{k,1});

Réponses (2)

Stephen23
Stephen23 le 16 Juin 2022
Modifié(e) : Stephen23 le 16 Juin 2022
EVAL is anti-pattern red-herring. Avoid EVAL.
The actual solution is to use the methods shown in the MATLAB documentation:
For example you can easily use the table.(expression) notation:
for ii = 1:numel(data)
for jj = 1:numel(c_data)
tbl = data{ii}; % clearer with a temporary variable
y = tbl.Svt_ActAng_A;
x = tbl.Sve_ActAng_A;
z = tbl.(c_data{jj});
end % ^^ ^ just like the documentation shows
end
This is simpler and much more efficient than what you were attempting using evil EVAL. And it works.
  1 commentaire
Steven Lord
Steven Lord le 16 Juin 2022
An approach I find even simpler is to just use the variable name as an index.
T = array2table(magic(4))
T = 4×4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
s = 'Var3';
asATableArray = T(:, s)
asATableArray = 4×1 table
Var3 ____ 3 10 6 15
asANumericArray = T{:, s}
asANumericArray = 4×1
3 10 6 15

Connectez-vous pour commenter.


ST
ST le 17 Juin 2022
Thanks Stephen53,
this line was the breaktrough:
tbl = data{ii}; % clearer with a temporary variable
As simple as it is :)

Community Treasure Hunt

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

Start Hunting!

Translated by