assign new names to matrices in loop

59 views (last 30 days)
Stefan
Stefan on 18 Feb 2015
Answered: Anjan on 21 Sep 2018
Hi,
I would like to rename a set of matrices, but the names should be flexible, so:
U1=USE1995
U2=USE1997
U3=USE1999
U4=USE2000
U5=USE2001
...
This code should become flexible, while the years can vary, but 1995 should lways be the first and then count until the last available year (and there could be any number of matrices). is it possible to make a LOOP for this (taking into accoutn that the names can always change)?
Thank you!!
  2 Comments
Stephen
Stephen on 20 Feb 2015
In case the message is not yet clear: do not use dynamically named variables, it makes doing anything in MATLAB very difficult, is hard to debug, etc. Do yourself a favor and use cell arrays or structures instead.

Sign in to comment.

Answers (4)

Stephen
Stephen on 18 Feb 2015
Edited: Stephen on 18 Feb 2015
When you are a beginner it seems like a cunning and fast way to store information, but actually it is really bad practice to name your variables dynamically. MATLAB is also not intended for this kind of variable naming: if you continue to include data in the variable names then you will find yourself fighting many more of these battles against MATLAB.
However when you use more appropriate storage for your data (and meta-data) then you will suddenly find lots of MATLAB functions that do many useful operations for you, quickly and easily.
In your case a much more robust solution would be to use structures , where you can include fields for each kind of data (e.g. Process type, Flow data, Temperature data, Notes, Units, etc). There are many functions that support working on structures and can access these data easily, and they can also be used in vectorized code (which is something you need to learn about). And yes, you can even define structure fieldnames dynamically .
Also note that structures are arrays, and can be non-scalar! Here is an idea of how you might store your data in a structure:
>> A(1).temp = [100,100,101,99];
>> A(2).temp = [80,90,100,110];
>> A(1).flow = [1,2,3,4;0,NaN,1,1];
>> A(2).flow = [-1,-1,0,0;Inf,Inf,0,0];
>> A(1).status = false;
>> A(2).status = true;
>> A(2).notes = 'This experiment was a lot of fun';
>> A(2).method = 'Laminar';
>> A(2).timestamp = [2015,2,3,19,35,48.238];
>> % etc
Using a structure makes accessing the data (even if you have non-scalar structures ) very easy, for example:
>> A(2).temp % the second experiment's temperature data
ans =
80 90 100 110
>> [A.status] % the status values for all experiments
ans =
0 1
Placing your data in a structure (or cell array, etc) also makes it much easier to pass to functions: can you imagine the fight you would have trying to pass hundreds of dynamically named variables to a function?
If you have a newer version of matlab you can also use a table , which stores the data together in one array but also allows key-name access to the columns. This might be a good alternative for your data.
In case you are interested, here are some pages explaining why dynamically assigning variable names is a really bad idea in MATLAB:

Anjan
Anjan on 21 Sep 2018
https://www.mathworks.com/matlabcentral/answers/194902-how-can-use-a-for-loop-to-name-a-matrix
This worked for me in Matlab 2018a

Sad Grad Student
Sad Grad Student on 18 Feb 2015
Agree with Stephen. However, if you have no choice, you can always start a loop with your conditions and sprintf should be able to give you the names and eval(<string>) should help you assign it to another variable. Not sure if this is what you should do though. There are more sophisticated ways to achieve what you want.

Stefan
Stefan on 19 Feb 2015
My problem is that I am editing a program which already uses variables named liek this: U1, U2, U3.... so it is very hard to adjust the whole program.
So, do you maybe still know how to code the above change of names of a series of matrices? I'm thinking of sth like this:
U1 = USE1995
U2 = USE1996, if not existent
U2 = USE1997
It seems indeed very difficult to tell Matlab something about the names of the matrices (in contrast to Stata).
Thank you!
  1 Comment
Stephen
Stephen on 20 Feb 2015
You are tell us that you are editing a program, which means that code can change, which means that it is not "very hard to adjust the whole program". Changing these poorly-named variables will be easier than trying to use them later in some calculations.
Use search-and-replace in the text editor, which will do something like this for you very quickly. Simply replace each
USE1995
with a structure reference:
data.USE1995
Because even if you manage to access those dynamically defined names, it still makes your life much harder because functions and operations in MATLAB are not designed for working with dynamically named variables. For example you will find it a major battle to do things like passing the data to a function, it also makes debugging very difficult, and it removes lots of useful tools such as code hinting, code error-checking, code completion and help links from the command line/text editor.
In future, never write your own code that creates or uses dynamic variable names.
PS: Please do not use Answers to write comments. The order of Answers is not guaranteed.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!