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

2 Ansichten (letzte 30 Tage)
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?

Akzeptierte Antwort

Wilson A N
Wilson A N am 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

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 19 Jan. 2017

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by