0

I want to do this with a shorter operation. How can i do it? Thank you

data = [Test1;Test2;Test3;Test4;Test5;Test6;Test7;---- until-----;Test36];
Dan
  • 45,079
  • 17
  • 88
  • 157
  • Why do your data look like that in the first place? If you explain where they comes from, it's possible there is a better way to create `data` earlier on... – Dan Oct 05 '15 at 15:31
  • Test1,Test2....are matrices 20x2 that i create in other function and i want to join the 36 matrices in 1 matrix 720x2 –  Oct 05 '15 at 15:35
  • 3
    So don't create them as separate matrices. Make them part of `data` in the first place. Consider making it a *20*-by-*2*-by-*36* matrix. Then you can just use `reshape` to get it into your *720*-by-*2* form if you need to later. Don't clutter your workspace with many variables that really should all be in the same container – Dan Oct 05 '15 at 15:37
  • 3
    If you show us how you're generating the 36 matrices, we'll try to give you a more reasonable approach. – beaker Oct 05 '15 at 15:41

2 Answers2

2

You can solve this using eval, however the use of this function is usually not recommended:

eval(['data=[', sprintf('Test%d;',(1:36)),'];'])

Rather follow Dan's comment and don't create seperate matrices.

m.s.
  • 16,063
  • 7
  • 53
  • 88
  • 2
    This is a really bad idea in this case (in almost every case) – Dan Oct 05 '15 at 15:39
  • @Dan why is it a bad idea? – Max Oct 05 '15 at 15:51
  • 2
    @Max check [this answer](http://stackoverflow.com/questions/32467029/how-to-put-these-images-together/32467170#32467170) and included links. They explain why using so-called "Dynamic Variable Names" is more or less the epitome of bad programming – Adriaan Oct 05 '15 at 15:54
  • 1
    @Max there are many articles online about this e.g. http://blogs.mathworks.com/loren/2005/12/28/evading-eval/. But in this case it is particularly poor advice because the solution is very clearly to have never created 36 different arrays in the first place. The OP has made a bad design choice which is easily rectifiable (note it's bad because it makes it extremely cumbersome to work with the data). This solution proposes a hacky work-around instead of addressing the root cause of the issue and recommending `eval` could cause miseducation. – Dan Oct 05 '15 at 15:56
  • ok, i got it, thanks – Max Oct 05 '15 at 16:05
  • 1
    Upvoted because of your disclaimer. I normally downvote `eval` questions because it's bad practice. – rayryeng Oct 05 '15 at 16:26
0

You can use the eval command to do this:

Test1=magic(5);
Test2=magic(5);
data=cell.empty(2,0);
for ii=1:2
    data{ii} = eval(sprintf('Test%d', ii));
end
Max
  • 1,471
  • 15
  • 37
  • What is `data=cell.empty(2,0);` for? I've never encountered this before, but when I test it, as soon as it runs `data{1} = ...`, `data` just becomes a *1*-by-*1* cell array so I can't see how it would help in terms of preallocation. Do you have a reference link to what `cell.empty` is for? I struggled to find it on Google... – Dan Oct 05 '15 at 16:02
  • I don't actually remember where i read it, but i read, that - in terms of efficiency - it's better to initialize a cell array outside of a for loop and you can do it by using `classname.empty`. Is that genereally wrong? http://de.mathworks.com/help/matlab/ref/empty.html – Max Oct 05 '15 at 16:08
  • I don't know if it's wrong, but I don't know that it's right which is why I was hoping you a link. I'm not sure that it does do anything but maybe it does. It would be good to see confirmation though. – Dan Oct 05 '15 at 16:16