How do I extract, sort and plot data from multiple individual .txt files?
5 次查看(过去 30 天)
显示 更早的评论
I have a specific problem that I've not been able to solve due to my lack of MatLab knowledge and would greatly appreciate any advice!
I am trying to simply plot 2 sets of variables from multiple earthquake events contained in the .txt files seen below. The data exists as 3 columns inside the text files, the distance in km (1st column of the below selected txt file) and the mmi respone (2nd column) and the 3rd column which is the standard deviation and can be ignored for now. The problem I have is doing this with all txt files and sorting them by size into grouped sets and plotting these on seperate graphs.

Here I have managed to plot two sets of this data by individually importing two .txt files into MatLab, assining variables based on the correct columns of each imported matrix array, inverting the column data to rows and plotting them. The trendline is made using the Curve Fitting Toolbox:

I now how to repeate this process for the other 61 .txt files and the question is how do I do this efficiently? Ideally using for loops and avoiding importing each txt file by hand. To parahrase the results I'm looking for:
- Import all .txt files from a folder (currently all 63, in the future there will be more) into the workspace (individual matrices or a single cell array?)
- Plot all data on a single graph, using only the 1st and 2nd column data of each individual txt file as the x-axis and y-axis values respectively. Then fitting a poly3 trendline based on all data that has been plotted.
- Then sort all imported txt file data sets into their respective magnitude sizes based on whole number increments given in the txt name ( i.e. Magitudes with values of 3 to 3.9, 4 to 4.9, 5 to 5.9 etc.) Then plot multiple graphs, one for each sorted set, plotting all events within those sets on the same graph and adding respectivly named trendlines. It would also be useful if each imported data set has its own correctly named trendline (based on specific Magnitude size, e.g. "4.6" from the txt file name) and colour!
This would help be a lot for my Civil Engineering research... not coming from a Computer Science background is making this very challening. Thank you again!!
0 个评论
回答(1 个)
KSSV
2020-8-20
To read all the text files:
txtFiles = dir("*.txt") ;
N = length(txtFiles) ;
for i = 1:N
thisFile = txtFiles(i).name ;
% Do what ever you want
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!