smarter and faster way to read several files

2 views (last 30 days)
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?

Answers (1)

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

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!