I have 163 text files. I need to take just the first two columns and average them, respectively. And then I need to repeat this for all files.
This is what the files look like:
-5.0814,-3.8986,28.66,-39.8
-5.1558,-3.8614,30.42,-40.44
-5.1602,-4.0198,32.98,-39.16
-5.0674,-3.9762,27.02,-38.6
-5.1074,-3.9094,31.62,-37.24
-5.0822,-4.0018,28.78,-40.68
-4.9494,-3.8082,31.02,-37.08
-4.9782,-3.7758,31.74,-39.64
-5.0362,-3.9478,30.9,-37.08
-5.2798,-4.0558,29.34,-39.64
The pattern: 000-int.txt, 001-int.txt, 002-int.txt, ..., 163-int.txt. There are gaps in the numbering. I have multiple copies of these files saved in different directories.
ATTEMPT: I tried using fopen/textscan/flclose, and even dlmread, etc. I couldn't get anything to work- at all. I could not figure out how to read one file, let alone create an average or repeat th开发者_如何学Ce process in some form of loop. My professor wanted me to help him with some data analysis.
Thank you in advance!
Stack Overflow is a site for asking programming questions. It is not appropriate to just ask us to write your code for you.
So, show us the code you tried and we might be able to help you fix it.
Also, you might want to check out Textscantool from the MATLAB File Exchange. It provides a GUI for importing formatted text files, and can generate code that will make the appropriate calls to textscan()
.
After having no time to work on it, I finally made some time. I came up with this:
Data = dlmread('000-int.txt', '', 'A1..B10');
M = mean(Data)
This reads only the first two columns of a file named "000-int.txt," and then takes the average.
The problem now is that I can't figure out how to make it do this for 162 more files. Actually, it is a bit less since there are gaps in the numbering. I have no idea where to start from, as I could not find anything online.
Thanks, and sorry for the delay and initial post that seemed like I was asking for someone to write my code for me. I was merely stating all the facts initially, so the readers/writers would have all the info necessary.
精彩评论