MATLAB Answers

Parsing text files for Beginners

8 views (last 30 days)
Justin Jiang
Justin Jiang on 24 Dec 2019
Commented: Justin Jiang on 16 Jan 2020
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 Comments

Show 9 older comments
Justin Jiang
Justin Jiang on 16 Jan 2020
Have you worked with K means clustering before?
Justin Jiang
Justin Jiang on 16 Jan 2020
Error using +
Integers can only be combined with integers of the same class, or scalar doubles.
Error in kmeans2>distfun (line 564)
D(:,i) = D(:,i) + (X(:,j) - C(i,j)).^2;
Error in kmeans2/loopBody (line 149)
minDist = min(minDist,distfun(X,C(ii-1,:),distance));
Error in internal.stats.parallel.smartForReduce (line 136)
reduce = loopbody(iter, S);
Error in kmeans2 (line 53)
ClusterBest = internal.stats.parallel.smartForReduce(...
Error in kmeans (line 322)
[varargout{1:nargout}] = kmeans2(X,k, distance, emptyact,reps,start,...
Error in Parsing (line 7)
idx = kmeans(out2,2); %out2 is the matrix ; 2 is the number of clusters
Capture2.JPG
Capture.JPG
Justin Jiang
Justin Jiang on 16 Jan 2020
After adding one line, the double casting made the code compile. Thanks for you help! Do you have any resources or links to direct me for the k means clustering? Anyways, this post is concluded as I have a good base now and code working. Thanks for being my first mentor as I am a complete novice in matlab.
Capture3.JPG

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 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
From there I can use normal table indexing to retrieve data.
>> 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 Comments

Show 2 older comments
Justin Jiang
Justin Jiang on 16 Jan 2020
Error using +
Integers can only be combined with integers of the same class, or scalar doubles.
Error in kmeans2>distfun (line 564)
D(:,i) = D(:,i) + (X(:,j) - C(i,j)).^2;
Error in kmeans2/loopBody (line 149)
minDist = min(minDist,distfun(X,C(ii-1,:),distance));
Error in internal.stats.parallel.smartForReduce (line 136)
reduce = loopbody(iter, S);
Error in kmeans2 (line 53)
ClusterBest = internal.stats.parallel.smartForReduce(...
Error in kmeans (line 322)
[varargout{1:nargout}] = kmeans2(X,k, distance, emptyact,reps,start,...
Error in Parsing (line 7)
idx = kmeans(out2,2); %out2 is the matrix ; 2 is the number of clusters
Capture2.JPG
Capture.JPG
Steven Lord
Steven Lord on 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})
Justin Jiang
Justin Jiang on 16 Jan 2020
After adding one line, the double casting made the code compile. Thanks for you help! Do you have any resources or links to direct me for the k means clustering? Anyways, this post is concluded as I have a good base now and code working. Thanks for being my first mentor as I am a complete novice in matlab.
Capture3.JPG

Sign in to comment.

Sign in to answer this question.