0

In Matlab, I want to select by name and loop through some elements in the workspace, let's say for example these matrixes:

mymatrix1=ones(2);
mymatrix2=zeros(2);
mymatrix3=magic(2);

I can select them using who:

list_of_mymatrixes=who('mymatrix*');

to do some operation, for example write them to respective files:

for i=1:length(list_of_mymatrixes)
    x=strcat("my_matrix_number_",string(i));
    dlmwrite(x,list_of_mymatrixes(i,1));
end

but they are stored as just the variable name. open('my_matrix_number_1') shows m,y,m,a,t,r,i,x,1

So, which is the way to loop through these elements so that they are recognized as matrixes instead of strings (their names)?

Faustino
  • 121
  • 5
  • 1
    Please don't use dynamical variable names. – Andras Deak -- Слава Україні May 22 '17 at 15:10
  • 2
    While it may seem that the duplicate is answering another thing, it is not. You have a problem arising from a premise that is wrong, so the marked duplicate helps you fox that premise, instead of the problem caused by it. – Ander Biguri May 22 '17 at 15:15
  • 1
    you can access to the variables by using `eval`. – OmG May 22 '17 at 15:15
  • 6
  • I am compelled to use dynamical variable names. The set of matrixes I actually intend to use derives from manually choosing image points with the "cpselect" tool for image registration. I compare successive pairs of images, obtaining a pertaining matrix of "movingPoints" that I have to name each time. – Faustino May 22 '17 at 18:33
  • 1
    @user3421755 use [a struct with dynamic field names](https://stackoverflow.com/questions/39959835/create-variables-for-indexing-in-matlab/39963723#39963723) instead. Whatever can be a variable name can also be a field name of a struct, but instead of having to kludge around with `eval` you can use the syntax `struc.('fieldname')`. If you start using dynamic variable names you set yourself on a path that's hard to get off of, and then you'll also say "I have to use dynamical field names because my data is set up like this..." – Andras Deak -- Слава Україні May 22 '17 at 19:02

0 Answers0