0

I have more than 30, 2D mat files name as A1, A2, A3,.... A30 in workspace. In order to create a 3D mat file how to call A1, A2,... in loop.

for i=1:30  
    A(:,:,i)=A{i}     
end

I want to make A{i} vary as A1,A2,A3,.. in the successive loop.

Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
  • 1
    Have you done some research on how to load `.mat` files in a loop? [here](http://stackoverflow.com/questions/10600519/loop-for-loading-and-saving-mat-files) for example? – Benoit_11 Oct 15 '15 at 13:32
  • yeah, but here I want to create a 3D matrix, which I am not getting. –  Oct 15 '15 at 13:42
  • @dSb don't use `eval`, rather use the method Benoit has linked to. Just load each mat file in your loop and then put it into your 3D matrix the way you already are doing... – Dan Oct 15 '15 at 13:52
  • 1
    If possible, you may want to change the generation of those 30 matrices so that they are automatically created into a single 3D array, or structure, cell whatever floats your boat and is a native MATLAB data container. Having 30 variables is usually cumbersome and breaks quite a few things in MATLAB, since `eval` is barely supported – Adriaan Oct 15 '15 at 13:55
  • @Dan how to load the mat file in my 3D matrix? A1=[1 2 3; 2 3 4; 3 4 5]; A2=A1*2; A3=A1*3; for i=1:3 f_out=strcat('A',num2str(i)) save (f_out) f_in=sprintf('%s%d.mat','A',i) A(:,:,i)=load (f_in); end –  Oct 15 '15 at 14:39
  • @dSb don't save them in separate variables in the first place! `A(:,:,1) = [1 2 3; 2 3 4; 3 4 5]; A(:,:,2)=A(:,:,1)*2` etc – Dan Oct 15 '15 at 14:46
  • @Dan Here I have shown an example, in actual each matfile has more than 1000 cells. So, I have to load each matrix seperately. –  Oct 15 '15 at 16:37

1 Answers1

0

You can use the evil eval, in combination with sprintf. That way you avoid using loops:

A1 = 1:3;A2 = 4:6; A3 = 10:12;
A = eval(['[',sprintf('A%i;',1:3),']'])
A =    
     1     2     3
     4     5     6
    10    11    12

The sprintf part creates a string A1;A2;A3;, like this:

sprintf('A%i;',1:3)
ans =    
A1;A2;A3;

Then, put brackets around it (it's still a string):

['[',sprintf('A%i;',1:3),']']
ans =
[A1;A2;A3;]

Finally, evaluate the string:

eval(['[',sprintf('A%i;',1:3),']'])

To work with 3D:

A1 = magic(3);A2=2*magic(3);A3=3*magic(3);
A = permute(reshape(eval(['[',sprintf('A%i;',1:3),']'])',3,3,[]),[2 1 3])
% or:
A = permute(reshape(eval(['[',sprintf('A%i;',1:3),']']),3,3,[]),[1 3 2])    
A(:,:,1) =    
     8     1     6
     3     5     7
     4     9     2        
A(:,:,2) =    
    16     2    12
     6    10    14
     8    18     4    
A(:,:,3) =    
    24     3    18
     9    15    21
    12    27     6

Up to the eval part, it's identical. Now, you need to reshape the transposed matrix so that it becomes a 3D matrix. Thereafter, you need to permute it, to switch the first and second dimension.


Note that you should try to avoid this. The readability is crap, the performance is crappier and the robustness is crappiest. If possible, you should definitely try to avoid the names A1, A2, A3 ....

Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70