Plotting multiple .txt files

조회 수: 8 (최근 30일)
Elham
Elham 2022년 7월 25일
댓글: Jon 2022년 7월 26일
Hi,
I have a folder with 1,638 items. They are all .txt files and each file has ~100,000 data points that I am using to plot spectrums with the following code;
%Which peak has the highest amplitude
fileID = fopen('file location\file name');
A = cell2mat(textscan(fileID,'%d','Delimiter',','));
fclose(fileID);
figure('Name','Amplitude')
plot(A(1:2048))
hold on
for i = 1:(numel(A)/2048)-1
data_segment = A(1+2048*i: 2048*(i+1));
z{i}=sum(data_segment);
z=z';
plot(data_segment)
end
hold off
%%Plot of the highest amplitude, ivMax=index of max
[VMax,ivMax]=max(A)
groupsize=2048;
start_index=floor(ivMax/groupsize)*groupsize
plot(A(start_index:(start_index+groupsize-1)),'LineWidth',1.5);
hold on
plot(ivMax-start_index, A(ivMax));
xlabel('Photon Energy(eV)','FontSize',11, 'FontWeight','bold')
ylabel('Harmonic Intensity(a.u.)','FontSize',11, 'FontWeight','bold')
title('Harmonic Intensity Using Ar','FontSize',12, 'FontWeight','bold')
set(gca,'FontWeight','bold','FontSize',10,'Color','white');
set(gca,'XTick',[])
xlim([0 2048])
%%Plot the area
b=(cell2mat(z));
maxz = find(max(b) == b);
figure('Name','Area')
hold on
plot(A(maxz*2048+1:(maxz+1)*2048),'LineWidth',1.5)
xlabel('Photon Energy(eV)','FontSize',11, 'FontWeight','bold')
ylabel('Harmonic Intensity(a.u.)','FontSize',11, 'FontWeight','bold')
title('Harmonic Intensity Using Ar','FontSize',12, 'FontWeight','bold')
set(gca,'FontWeight','bold','FontSize',10,'Color','white');
set(gca,'XTick',[])
hold off
xlim([0 2048])
I have to go through each file to see which spectrum has the highest amplitude. Is there a way to allow matlab to read all the files and only plot the spectrum with the highest Vmax? Or a way to plot all of the spectrum onto one figure and read which file has the largest value?

답변 (1개)

Jon
Jon 2022년 7월 25일
편집: Jon 2022년 7월 25일
You could do something like what is shown in this simplified example
% Get a list of all of the text files
list = dir('*.txt')
% Loop through the text files, reading each one, and find maximum element
numFiles = numel(list); % total number of data files
peakValue = zeros(numFiles,1); % preallocate array to hold peak value of each file
for k = 1:numel(list)
% Read the data into a matrix
A = readmatrix(list(k).name);
% Find the maximum value and save it
peakValue(k) = max(A(:));
end
% Find the file that has the largest peak
[maxPeak,idx] = max(peakValue);
% need to read data back in for the selected file (one with the largest peak)
A = readmatrix(list(idx).name);
% plot the file with largest peak
plot(A)
I'm assuming you have too much data to read all of the data into a large array and keep it in memory, that is why I read the files one at a time and just keep the peak value from each.
Also, note that I use readmatrix to read in the text file data. If your formatting isn't too complicated (just comma separated, you should be able to do this rather than reading in one line at time.
  댓글 수: 4
Elham
Elham 2022년 7월 25일
Thanks @Jon
Jon
Jon 2022년 7월 26일
Are you all set now, or do you still have some questions on how to move ahead? If this answered your question please accept the answer.

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

카테고리

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