Parsing text files for Beginners
63 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am new to Matlab. Looking for something simple.
This is my data
14:27:45.535 -> Color Temp: 9888 K - Lux: 9155 - R: 23431 G: 20757 B: 21860 C: 47209
14:27:46.258 -> Color Temp: 9813 K - Lux: 9317 - R: 23760 G: 21059 B: 22144 C: 47781
14:27:46.979 -> Color Temp: 9111 K - Lux: 10240 - R: 25986 G: 22832 B: 23719 C: 50101
14:27:47.668 -> Color Temp: 7065 K - Lux: 6485 - R: 18168 G: 14743 B: 14873 C: 26192
14:27:48.394 -> Color Temp: 6879 K - Lux: 6104 - R: 17014 G: 13777 B: 13823 C: 24495
14:27:49.084 -> Color Temp: 6760 K - Lux: 8271 - R: 22906 G: 18545 B: 18530 C: 33597
14:27:49.813 -> Color Temp: 7217 K - Lux: 8778 - R: 24217 G: 19835 B: 20038 C: 37326
14:27:50.500 -> Color Temp: 7352 K - Lux: 9219 - R: 25368 G: 20858 B: 21131 C: 39681
14:27:51.223 -> Color Temp: 7360 K - Lux: 9070 - R: 24427 G: 20262 B: 20467 C: 38348
I want to parse R,G,B,C into a 4dimensional matrix.
Please help, the online examples are too hard for a novice like me.
12 commentaires
Réponses (1)
Steven Lord
le 15 Jan 2020
There are several ways to read this data in. While you can do this using a series of commands, an easier approach (especially if you only need to read this file in once) is to use the interactive data importing tool. I did this for your file and the steps I followed were:
- Open the file in the Import Tool
- Tell MATLAB that the file is space-delimited rather than fixed-width
- Select (using Ctrl-click) just "important" columns (A, E, I, L, N, P, and R)
- Give those selected columns informative names (Time, Temp, Lux, R, G, B, and C)
- Modify the format of column A slightly, to add in the fractional seconds
- Tell MATLAB to import the selected data.
Here's what the result of that importing looked like as a table array.
sampledata =
9×7 table
Time Temp Lux R G B C
____________ ____ _____ _____ _____ _____ _____
14:27:45.535 9888 9155 23431 20757 21860 47209
14:27:46.258 9813 9317 23760 21059 22144 47781
14:27:46.979 9111 10240 25986 22832 23719 50101
14:27:47.668 7065 6485 18168 14743 14873 26192
14:27:48.394 6879 6104 17014 13777 13823 24495
14:27:49.084 6760 8271 22906 18545 18530 33597
14:27:49.813 7217 8778 24217 19835 20038 37326
14:27:50.500 7352 9219 25368 20858 21131 39681
14:27:51.223 7360 9070 24427 20262 20467 38348
>> sampledata{5, 'R'}
ans =
17014
>> R = sampledata.R;
If you need to import multiple files with that same format, you could use Import Tool on the first file to get the formatting handled correctly then generate code from the Import Tool to read in the rest of the files.
If you want to see how you would do this purely via commands, I can show that.
5 commentaires
Steven Lord
le 16 Jan 2020
What's the class of out2?
If it's a table array or a cell array, use class to determine the class of the first variable in the table or the first cell in the cell array. The following should work:
class(out2{1, 1})
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!