필터 지우기
필터 지우기

loop through files not working correctly.

조회 수: 2 (최근 30일)
John
John 2012년 3월 18일
Hello,
I'm trying to convert .xls files in a folder into .mat files, but the code is not looping through the files correctly.
The .xls files are called cycle1, cycle2, cycle3...etc and they all have different numbers of rows.
sch_cycle and nrows are not changing for each file. It creates all the .mat files but they are all the same corresponding to cycle1.xls.
Could anybody tell me what is going wrong?
Sincere thanks
John
x = cellstr(ls('*.xls'));
for k = 1:length(x)
sch_cycle=xlsread('C:\Autonomie practice\cycle1.xls','Input_data');
nrows = size(sch_cycle,1)-1;
sch_grade=[0,0;nrows,0];
sch_grade=[0 0;nrows 0];
sch_key_on=[0 1; nrows 1];
[~,fn] = fileparts(x{k});
sch_metadata.name = fn;
sch_metadata.proprietary='public';
save([fn,'.mat'],'sch_cycle','sch_grade','sch_key_on','sch_metadata');
end

채택된 답변

David Young
David Young 2012년 3월 18일
Your code reads cycle1.xls every time it starts the loop. It needs to read each file in turn - probably like this:
sch_cycle = xlsread(x{k}, 'Input_data');
since the call to ls suggests that you expect to be working in the directory with the .xls files.
  댓글 수: 1
John
John 2012년 3월 18일
Oh, I should of realized that! Thanks

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by