How to load specific files according to vector values?

조회 수: 4 (최근 30일)
Sigal Cohen
Sigal Cohen 2017년 11월 7일
편집: Guillaume 2017년 11월 7일
I have a vector ii(1:20):
ii(1:20)
ans =
2
5
9
10
11
15
16
17
18
20
1
3
4
6
7
8
12
13
14
19
I want to load files according to the values inside the vector, i.e.: to load file called '2.mat', '5.mat', '9.mat' and so on. But I need only the first 10 values of ii (ii(1:10)) (then I combine them in a struct..but that's not relevant).
So this is what I have so far:
for ii=???
load(sprintf('%d.mat', a));
Struct(n).file=Cycle;
end
So how do I load the first 10 values from the vector?
Thank you all!!

채택된 답변

Guillaume
Guillaume 2017년 11월 7일
편집: Guillaume 2017년 11월 7일
First thing, use better variable names. ii is really bad, it does not mean anything and looks like a temporary variable. Let's call it filenumber instead so it's immediately clear what the purpose of the variable is.
filenumber = [2 5 9 10 11 15 16 17 18 20 1 3 4 6 7 8 12 13 14 19];
filestoload = 10;
cycles = struct('Cycle', cell(1, filestoload)); %predeclare structure
for idx = 1:filestoload;
filecontent = load(sprintf('%d.mat, filenumber(idx))); %avoid plain load, always load into a variable
cycles(idx).Cycle = filecontent.Cycle;
end

추가 답변 (2개)

KL
KL 2017년 11월 7일
Something like this?
for k = 1:10
data = load([num2str(ii(k)) '.mat']);
dataStruct(k).file=data.Cycle;
end

KSSV
KSSV 2017년 11월 7일
Let ii be your above vector.
for i = 1:10
filename = strcat(num2str(ii(i)),'.mat')
load(filename);
end

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by