Astrolink
Astrolink

Reputation: 3

How to read sequentially named variables in workspace of MATLAB

I have data for lightning and I would like to read them in a loop, perform the row index on each variable given by;

    row_idx = (hr == M(:, 2) & -40 <= M(:, 3) & M(:, 3) <= 0 & 135 <= M(:, 4) & M(:, 4) <= 180... 
| (-180 < M(:, 4) & M(:, 4) <= -120)); filtered_M = M(row_idx, :);

where M is a dummy variable which has been assigned with the value of the variable, and hr is the hour (which I will already assign as a global variable).

The variables are named as A(year)(month)(day) e.g., A20130101 for data of 1st January 2013, and A20131229 for 29th December 2013 data.

My aim is to read all the variables in a for loop. As each variable is read, I would like to perform the row index operation (mentioned above) on them and store the manipulated variables.
For instance if the read variable is A20130201, then I would like to store the manipulated variable as filtered_A20130201.

I have difficulty reading the variables and storing the resulting matrices.

I have tried this,

for k = 1:n  
  M = A2013010k;  
  row_idx = (hr == M(:, 2) & -40 <= M(:, 3) & M(:, 3) <= 0 & 135 <= M(:, 4) & M(:, 4) <= 180 ...  
 | (-180 < M(:, 4) & M(:, 4) <= -120));  
  filtered_M = M(row_idx, :);  
  filtered_A2013010k = filtered_M;  
end

but realize my efforts are in vain as I am not familiar with MATLAB coding.

Upvotes: 0

Views: 541

Answers (2)

Hoki
Hoki

Reputation: 11792

You cannot simply use numeric variables to construct direct variable name. A workaround is to construct a string of the variable name, then calling the function eval.

For example:

for k = 1:n
   varName = ['A201401' sprintf('%02d',k)] ;
   eval( ['M=' varName ] ) ;

   row_idx = (hr == M(:, 2) & -40 <= M(:, 3) & M(:, 3) <= 0 & 135 <= M(:, 4) & M(:, 4) <= 180 ...  
 | (-180 < M(:, 4) & M(:, 4) <= -120));  

   filtered.(varName) = M(row_idx, :) ;
end

Although the use of eval is not recommended.

To avoid running into the same issue (having to use eval) to store your filtered variable, I assigned them into a structure. The matlab structures can have dynamic field, it means you can construct the name of the field based on strings (which are easy to construct or increment programmatically).

Have a look at the accepted answer for this question if you want to understand more. It is very similar to what you are asking and the answer is well explained.

Upvotes: 1

shinjin
shinjin

Reputation: 3027

It's generally bad practice to have a sequence of data in a sequence of variables, as you have for A20130101, etc.

In case it cannot be helped, you can use eval as a workaround:

for k = 1:n
  eval(sprintf('M = A201301%02d', k));
  row_idx = (hr == M(:, 2) & -40 <= M(:, 3) & M(:, 3) <= 0 & 135 <= M(:, 4) & M(:, 4) <= 180 ...  
 | (-180 < M(:, 4) & M(:, 4) <= -120));  
  filtered_M = M(row_idx, :);  
  eval(sprintf('filtered_A201301%02d = filtered_M', k));
end

See this page for more details and best practices.

Upvotes: 1

Related Questions