smarter and faster way to read several files

조회 수: 2 (최근 30일)
aneps
aneps 2016년 5월 2일
답변: Walter Roberson 2016년 5월 2일
I have several data files (.dat) on which I want to perform the following code:
NL=8;
Name = 'Output_1';
filename = [Name,'.dat'];
if ~exist(filename, 'file')
error('Your data file %s does not exist in this directory', filename);
end
[fid, message] = fopen(filename, 'rt');
if fid < 0
error('Opening the file %s failed because %s', filename, message);
end
Data = cell2mat( textscan(fid, '%f%f%f', 'Delimiter', '\t', 'TreatAsEmpty', ...
'nil', 'EmptyValue', 0, 'CollectOutput', 1,'headerlines',NL) );
fclose(fid);
m1=300;
m2=600;
index_1 = find(Data(:,1)==m1);
Data_1=Data(index_1,:);
Data_m1=[Data_1(:,1) Data_1(:,2)];
index_2 = find(Data(:,1)==m2);
Data_2=Data(index_2,:);
Data_m2=[Data_2(:,1) Data_2(:,2)];
nbins = min(Data_1(:,2)):1:max(Data_2(:,2));
[NX1 X]=hist(Data_1(:,2),nbins);
figure(1)
plot(X,NX1,'b')
hold on
[NX2 X]=hist(Data_2(:,2),nbins);
figure(1)
plot(X,NX2,'r')
Here I am reading the data from file with name Output_1 and find those indices corresponding to X= 300 and 600. Then plot the histogram of corresponding Y. How can I loop this to perform the same operation on several files named Output_1, Output_2....Output_n and then accumulate the plot with different colors or markers to identify the files?

답변 (1개)

Walter Roberson
Walter Roberson 2016년 5월 2일
You might want to look at plt() from the File Exchange as it makes it easier to create different combinations to distinguish lines.

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by