How can I use a string to call a matrix with the same name?

Hi all,
I have a dynamic string, begin (depending on n) either Radiation1, Radiation2, Radiation3, etc. I also have a ton of matrices in my workspace, also called Radiation1, Radition2, etc.
What I now want is depending on what the string reads, to open a certain matrix, read a part of it, and put it into another matrix called Radiation.
So:
I have Radiationn = ['Radiation' int2str(n)] and the outcome of this is for example that Radiationn = Radiation2.
I now want a matrix called Radiation to be filled with certain data from Radiation2 (depending on lots of other stuff, but I got that figured out :-) ).
How can I get the string to relate to the name of the matrix? The piece of code I have now is:
for n = 1:20
while (lfpt - n*500) > -499
if (fp1 - n*500) < 0
Radiationn = ['Radiation' int2str(n)];
Radiation(:,:,1) = Radiationn(:,:,(fp1-(n-1)*500));
end
end
end
A small legend here might be in place:
lfpt is an integer depending on the loaded matfile (not a fixed number, it might vary with the matfile)
fp1 is an integer depending on other input data (also a varying number - I also have fp2, fp3 and fp4, I might solve the above problem with this as well if possible!)
Radiation1:20 are very large multi-dimensional matrices (too large to put into one!)
Radiation is a smaller multi-dimensional matrix containing only the necessary data from the large matrices for the follow-up part of the program.
If you could help me out here, I would be very, very grateful!
Jessica

More Answers (1)

Simplest option I can think of is to replace your ton of arrays in the workspace with a ton of arrays attached to a struct.
Then you can use the following notation to access the relevant matrix from your struct:
Radiationn = myStruct.( ['Radiation' int2str(n)] )

Categories

Asked:

on 5 Aug 2014

Edited:

on 12 Sep 2023

Community Treasure Hunt

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

Start Hunting!