フィルターのクリア

how to read different columns of data from tab limited text file using matlab textscan

3 ビュー (過去 30 日間)
Hi All, I want to extract column 2,6 and 7 and save to another text file using textscan function .
I tried below code .
data5=fopen('chennel.txt','r');
store=fopen('raw.txt','w');
while ~feof(data5)
line = fgetl(data5);
data_extract= textscan(data5,'%d %d %s %s %s %d %d %d %s','delimiter','');
A=[data_extract{2},data_extract{6},data_extract{7}];
fprintf(store,'%d\t\t%d\t\t%d\n',A');
end
fclose(data5);
fclose(store);
%Error is :
Error using feof
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in plot_channel1 (line 3)
while ~feof(data5)
chennel1.txt contains
1 7 BS Y E- 618 464 49 GH
1 7 BS Y E- 618 464 49 GH
1 7 BS Y E- 618 463 49 GH
1 7 BS Y E- 618 463 49 GH
1 7 BS Y E- 618 463 49 GH
1 7 BS Y E- 618 463 49 GH
1 7 BS Y E- 618 463 49 GH
1 7 BS Y E- 618 463 49 GH
1 7 BS Y E- 618 463 49 GH
1 7 BS Y E- 618 463 49 GH
1 7 BS Y E- 618 462 49 GH
What is the mistake I am doing here.
Any suggestions please.
Thanks
  4 件のコメント
Stephen23
Stephen23 2017 年 8 月 5 日
@POKA: that error is unrelated to your question. You should accept Walter Roberson's answer below because it answers your original question.
To fix the new error you will have to look at the size of each array that is being concatenated together.
POKA
POKA 2017 年 8 月 5 日
編集済み: Walter Roberson 2017 年 8 月 5 日
okay I agree.

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

採用された回答

Walter Roberson
Walter Roberson 2017 年 8 月 5 日
編集済み: Walter Roberson 2017 年 8 月 5 日
Your
data5=fopen('chennel.txt','r');
failed. Perhaps the file name is 'channel.txt'. Or perhaps it is not in the current directory or on the MATLAB path.
You should use
projectdir = pwd; %if you do not expect it to be in the current directory then adjust this
filename = fullfile(projectdir, 'chennel.txt');
[data5, msg] = fopen(filename, 'r');
if data5 < 0;
error('Failed to open file "%s" because: "%s"', filename, msg);
end
  2 件のコメント
POKA
POKA 2017 年 8 月 5 日
編集済み: Walter Roberson 2017 年 8 月 5 日
Yes I know spelling mistake but it is chennel1.txt only.
Walter Roberson
Walter Roberson 2017 年 8 月 5 日
With regard to your code,
fid=fopen('chennel1.txt','r');
data_extract= textscan(fid,'%d %d %s %s %s %d %d %d %s','delimiter','');
A=[data_extract{1},data_extract{6},data_extract{7}];
you are getting inconsistent array dimensions because the number of rows for data_extract{1} is not the same for the number of rows for data_extract{6} or data_extract{7}. This can happen with textscan if the textscan() finishes early on the final line that it reads in, which can happen if there is a mis-match between the format specification and the input data. For example if a string appeared in a position where a number was expected and that was not the first format item on the line, then the earlier items on the line would have been filled in with whatever was read, but that item and the later ones on the same line would be empty.
Your use of 'delimiter', '' is almost certainly wrong. Note that spaces in the input format between the input items are ignored, so if you expect a space between each item, you should not expect it to be handled by matching the spaces in the input format: you should instead leave 'delimiter' out of the options to allow it to default to whitespace.
When you set 'delimiter', '' then the %s format matches until end of line. Each of the two %s after that would fill in '' for that position. The %d after that might get filled in with 0 but not if there are no more \n in the input file; in that case it would be empty.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by