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

조회 수: 2 (최근 30일)
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?

채택된 답변

Wilson A N
Wilson A N 2017년 1월 19일
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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 1월 19일

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by