Can you help me with this easy loop task?

조회 수: 2 (최근 30일)
Peter
Peter 2015년 1월 2일
댓글: Peter 2015년 1월 2일
Hello i need some help with an easy task.
I have a folder called rpm_measurements and inside of it a lot of folders called rpm0x00 (for example in the code below you can see rpm0800).
What i need is a loop to make the process you see but for each folder of my rpm_measurements folder , not only for one, as it is below.
Can you help me?
workfolder='C:\Users\Jorge\Desktop\Work_Christmas\Measurements\rpm_measurements\rpm0800\';
folders=[
{'cdaq2mod2_ai0-hw\'}
{'cdaq2mod2_ai1-hw\'}
];
file_data=dir([workfolder folders{1} 'x*.dat']);
for i=1:length(file_data);
files(i,1)={file_data(i).name};
end
delimiterIn='\t';
headerlinesIn=11;
for i=1:length(files)
for k=1:length(folders)
filepath = [workfolder folders{k} files{i,1}];
A=importdata(filepath,delimiterIn,headerlinesIn);
vel(:,k,i)=A.data(:,3);
end
end

채택된 답변

Geoff Hayes
Geoff Hayes 2015년 1월 2일
Peter - what are the valid values for x? For example, if x can be just 1 through 9, then you could create your workfolder variable given x as
for x=1:9
workfolder = sprintf(['C:\Users\Jorge\Desktop\Work_Christmas\'...
'Measurements\rpm_measurements\rpm0%d00\'],x);
% do other stuff that you have shown above
end
The "other stuff" would be the code that you have shown in your question, but you would have to modify it so that the output from each of these folders, vel, is saved to (perhaps) a cell array.
  댓글 수: 3
Geoff Hayes
Geoff Hayes 2015년 1월 2일
Peter - rather than making vel a cell array, just create something else that will contain all the vel data that is created at each iteration. Try the following
myData = cell(14,1);
for x=100:100:1400
workfolder = sprintf(['C:\Users\Jorge\Desktop\Work_Christmas\'...
'Measurements\rpm_measurements\rpm%04d\'],x)
% do other stuff that creates vel
% save vel to myData
myData{k} = vel;
end
Note how the sprintf determines the number of zeros that should be used to prefix the integer portion of the folder. We use %04d to indicate that there should be at most four zeros in the number.
Peter
Peter 2015년 1월 2일
Okay, thanks a lot.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 1월 2일
Attached is some code that might help. It will recurse into all subfolders and find the files. You won't have to know the folder names and hard code them in like you did.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by