Azzera filtri
Azzera filtri

How do I create an array containing the names of all the variables in a Workspace and then use this array to run a function/script on all the variables?

38 visualizzazioni (ultimi 30 giorni)
I have a workspace with about 200 tables. I frequently need to run similar operations (scripts) on all the tables.
  1. First, is there a way to create an array that automatically pulls the names of the tables?
  2. Second, no matter if the first one is possible or not; if I have a cell array containing all the names of the tables is there a way that can call these names and apply operations/scripts on them.
For example If I have the tables x_1, x_2, x_3 ... , x_200 in the workspace.
var_names = {'x_1'; 'x_2'; 'x_3' ... 'x_200'}; %is there an easier way to do this.
% if I want for example to multiply the first column in the three variable, I currently have to do it like this
%
x_1{:, 1}= x_1{:, 1} * 100;
x_2{:, 1}= x_2{:, 1} * 100;
x_3{:, 1}= x_3{:, 1} * 100;
...
x_200{:, 1}= x_200{:, 1} * 100;
%
% is there anoter way to use a loop to call all the variables using the var_names array? It should in principle look like this.
%
for n = 1:200;
"something(n)" = "something(n)" * 100;
end
Thanks in advance.
Best
Antonius
  11 Commenti
Antonius Armanious
Antonius Armanious il 2 Mar 2017
Forget about this, it was just an example. In my real script, that would look like
fr_CYS_pH7_I10_RAW_4133.Time = fr_CYS_pH7_I10_RAW_4133.Time/60;
Walter Roberson
Walter Roberson il 2 Mar 2017
Assign the output of whos to a variable. The result will be a structure array that has a name field. You can use structure expansion to tosd the content of the fields into one cell array.

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 2 Mar 2017
  9 Commenti
Stephen23
Stephen23 il 2 Mar 2017
Modificato: Stephen23 il 2 Mar 2017
@Antonius Armanious: I know how it is, when experimental data is stored with some meta-data in the filename:
test_80kmh_25degC_900amps.csv
That may be fine for storing the data in files. This is intuitive and obvious to humans when creating those files, but when importing that data into MATLAB (or most other programming languages) it would be a poor practice to try and keep those names as variable names (for the reasons give in our comments, and the links I gave). Instead, that meta-data should be parsed and imported as data in its own right (which makes it easier to work with too!), and all of the measured data imported into one simple variable using indexing (or table rows, fieldnames, etc).

Accedi per commentare.

Più risposte (1)

per isakson
per isakson il 2 Mar 2017
Modificato: per isakson il 2 Mar 2017
I guess you got the message. (I might delete this answer.) Try this, which is a solution if you painted yourself into a corner.
T = table(categorical({'M';'F';'M'}),[45;32;34],...
{'NY';'CA';'MA'},logical([1;0;0]),...
'VariableNames',{'Gender' 'Age' 'State' 'Vote'});
fr_Au_pH7_I10_RAW_9999 = T;
fr_CYS_pH8_I50_RAW_9999 = T;
MS2_COOH_pH7_I10_RAW_9999 = T;
sas = whos( '-regexp', '.+?RAW_\d{4}' );
sas = reshape( sas, 1,[] );
MyTables = struct('Promise', 'I will never do this again! ');
for s = sas
MyTables.(s.name) = eval( s.name );
end
name_list = fieldnames( MyTables );
name_list(1) = [];
name_list = reshape( name_list, 1,[] );
for name = name_list
MyTables.(name{:}) = foo( MyTables.(name{:}), 17 );
end
where
function T = foo( T, N )
T.Age = T.Age + N;
end
  4 Commenti
Stephen23
Stephen23 il 2 Mar 2017
@Antonius Armanious: what per isakson showed is how much simpler it is to work with data when it is stored correctly: using some very simple syntax per isakson compared data, and selected a subset of that data based on the comparison. Using lots of variables with complicated names would make this task very complex, slow, and buggy.
That is why we have been telling you to use better data storage (i.e. variables): because it makes your code better!
Antonius Armanious
Antonius Armanious il 3 Mar 2017
@per isakson and @Stephen Cobeldick. Thanks again. I just need to get myself familiarized with using structures to organize my data. I think I have a steep learning curve but I will follow your advice.

Accedi per commentare.

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by