フィルターのクリア

Reading from a text file

2 ビュー (過去 30 日間)
Usman Azher
Usman Azher 2011 年 4 月 27 日
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

採用された回答

Chirag Gupta
Chirag Gupta 2011 年 4 月 27 日
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 件のコメント
Usman Azher
Usman Azher 2011 年 4 月 27 日
I believe the row of zeros is showing because of additional spaces in the data
Chirag Gupta
Chirag Gupta 2011 年 4 月 27 日
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

サインインしてコメントする。

その他の回答 (1 件)

Usman Azher
Usman Azher 2011 年 4 月 27 日
If anyone wants to see the entire text file, I can email it to you.

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by