Plotting multiple .txt files
3 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!