How can I load multiple dat files of different name pattern and from different directory consecutively to do something
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi, I have some dat files of row*Column( 9999*10 )of names:
dir1/a_0.1_1
dir1/b_0.1_2
dir1/c_0.3_1
dir1/c_1.5_1
dir2/a_0.1_1
dir2/a_0.5_1
Now How can I load and recall then sequentially to do something? is there any short cut for loop for this specially when they are in different folder? Thank you
댓글 수: 0
채택된 답변
Ameer Hamza
2018년 5월 16일
You can use this FEX submission. Download it and place it in MATLAB path. To get the list of all .dat files, use it as follow
files = subdir('*.mat');
this will work, if you are present in the top folder of dir1, dir2 etc. This will give you name and path of all the '.dat' files in the subfolder. Then read and process like this
data = cell(1, length(files))
for i=1:length(files)
filename = files(i).name;
% load your file here, use load(), tableread() or any appropriate function depending on the type of data in .dat files
data{i} = readData; % save your read data or do any other processing
end
댓글 수: 7
Ameer Hamza
2018년 5월 21일
You are welcome. Yes, I noticed that your code requires huge memory several 10s of GBs. I would have taken a long time. You can further decrease time by making interpolation resolution of 10^8 to smaller values, but this depends on your requirement
Ameer Hamza
2018년 5월 21일
In case you haven't noticed, the 2 lines at end should be
ave_time_extr = ave_time/length(files);
ave_radius_extr = ave_radius/length(files);
I forget to change those lines. Therefore right now you are just adding all the columns of X and Y. To get mean value, you need to divide them with length(files).
추가 답변 (5개)
az
2018년 5월 22일
댓글 수: 3
Ameer Hamza
2018년 5월 22일
For NaN you can use fillmissing() to replace values. Since you also want to deal with inf in the same way, so first replace all inf with NaN and then use fillmissing(). For example
x = [1 2 inf 8 9 nan 15];
x(isinf(x)) = nan;
fillmissing(x, 'linear') % using linear will avoid repeated values.
ans =
1 2 5 8 9 12 15
az
2018년 5월 23일
댓글 수: 1
Ameer Hamza
2018년 5월 23일
Can you explain again, which variable have different lengths and which function is causing the error?
az
2018년 5월 23일
댓글 수: 4
Ameer Hamza
2018년 5월 23일
No, You must have all columns of equal length to take mean. Mean will only make sense if all columns have equal lengths. That is why you need to use fixed interpolation.
참고 항목
카테고리
Help Center 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!