How can I automatically load multiple data files and insert it into one matrix?

3 vues (au cours des 30 derniers jours)
I have 10 .mat files with 5 variables each. I want to load them automatically in MATLAB into one Matrix were the values of the variables are inserted one after another. And I want to insert a counter on the right that tells me the number of the file.
[Var1 Var2 Var3 Var4 Var5 Counter
Valfile1 Valfile1 Valfile1 Valfile1 Valfile1 1
Valfile1 Valfile1 Valfile1 Valfile1 Valfile1 1
Valfile1 Valfile1 Valfile1 Valfile1 Valfile1 1
Valfile2 Valfile2 Valfile2 Valfile2 Valfile2 2
Valfile2 Valfile2 Valfile2 Valfile2 Valfile2 2
Valfile2 Valfile2 Valfile2 Valfile2 Valfile2 2
...
Is that possible with a for loop?

Réponse acceptée

Wilson A N
Wilson A N le 19 Jan 2017
Yes it is possible. You just have to print the index indicating from which file you are currently loading the data. You can see the sample code given below:
k = [];
FileName = {'file1.mat','file2.mat','file3.mat'};
for i = 1:3
k = [k; importdata(FileName{i}) i*ones(5,1)];
end

Plus de réponses (1)

Walter Roberson
Walter Roberson le 19 Jan 2017

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by