Reading from a text file
1 view (last 30 days)
Show older comments
I have data in a text file that looks like as follows:
A 736 575 493 669 517 514 519 Z
A 741 482 518 721 510 518 495 Z
A 746 540 518 696 541 521 480 Z
A 747 490 470 740 544 518 477 Z
748 468 522 674 502 517 465 Z
A 749 551 546 627 500 512 472 Z
A 750 454 521 653 538 503 474 Z
A 751 454 555 711 540 508 467 Z
A 752 615 551 740 532 508 482 Z
A 753 471 477 853 526 507 487 Z
As you can see the first four rows are the same where as the fifth is not. Is there any way I can only read the rows that are similar and neglect the rest? In addition, I would only like to read the columns 3,4,5,6,7,8. I tried using the following code but it give me an error I cannot figure out.
[a_x,a_y,a_z,pitch,roll,yaw]=textread('data3.txt','%*s %*d %d %d %d %d %d %d %*s','headerlines',1);
The error states that Trouble reading integer from file (row 698, field 8) ==> Z\n
I have, of course, not shown the entire data. Your help would be greatly appreciated. Thank you
0 Comments
Accepted Answer
Chirag Gupta
on 27 Apr 2011
function rev_data = sampleread
filename = 'data.txt';
fid = fopen('data.txt','r');
% Use TEXTSCAN and not textread
M = textscan(fid,'%d %*d %d %d %d %d %d %d %d','TreatAsEmpty',{'A','Z'})
fclose(fid);
% Convert data to Matrix (since I am not interested in A and Z)
data = cell2mat(M);
% eliminate dissimilar rows
rev_data = data(~data(:,1),:);
% filter out dummy columns for A and Z
rev_data = rev_data(:,2:7);
This should do the trick. It works for the sample data you have posted. Read the documentation on TEXTSCAN.
doc textscan
7 Comments
Chirag Gupta
on 27 Apr 2011
Yup, the data file has other formatting issues.
You have two options:
1) Format the data (if its going to be onetime thing)
2) Remove these rows that return 0
Sorry, there doesn't seem to be an easier option
More Answers (1)
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!