Concatenating a large number or matrices

8 visualizzazioni (ultimi 30 giorni)
Nathaniel Fillman
Nathaniel Fillman il 19 Feb 2019
Commentato: Stephen23 il 19 Feb 2019
So I have a number of matrices, most of which have a common naming convention (temp_Xkm). Each was originally a file that was loaded in (of the format: temp_Xkm.dat). There is one file name t1.dat.
I would like to easily concatenate them (assuming that a loop is easier). Below is what I have for the long way, handjamming it in, and the short way, trying to loop it.
My loop attempt has been unsuccessful and I'm lost on where to go from here.
Thanks for any help!
%the long way
T3D = cat(11, t1, temp_1km, temp_2km, temp_3km, temp_4km, temp_8km...
, temp_11km, temp_14km, temp_15km, temp_16km, temp_17km);
%the short way?
for Tii = [1,2,3,4,8,11,14,15,16,17];
TC = cat(11, t1, ['temp_' num2str(Tii) 'km'])
Alltemps_vec(Tii)= TC;
end
  3 Commenti
KSSV
KSSV il 19 Feb 2019
YOu hav to read the .dat files ina loop and join them.......the way you are following is not correct...
Stephen23
Stephen23 il 19 Feb 2019
"My loop attempt has been unsuccessful and I'm lost on where to go from here."
Change how you import that data. Instead of dynamically creating lots of variable names simply import the data into one array using indexing, exactly as the MATLAB documentation shows:
Simple indexing is neat, trivially easy to use, and very efficient, unlike what you are trying to do.
Dynamically accessing variable names is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know some of the reasons why:

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by