how to read a txt file having alphanumeric data and special characters. ? My text file is like this. I tried textread but could not get a result. any suggestion.

1 6:s 6:s 0 0 0 0.11 0 *0.889
2 6:s 6:s 0 0.272 0 0.006 0 *0.722
3 6:s 6:s 0 0.036 0 0 0.001 *0.963
4 6:s 6:s 0 0 0 0 0 *1
5 6:s 6:s 0 0 0 0 0 *1
6 6:s 3:f + 0 0.238 *0.365 0.009 0.033 0.355
7 6:s 5:n + 0 0 0 0 *0.999 0.001
8 6:s 6:s 0 0 0 0 0 *1

3 件のコメント

jonas
jonas 2018 年 8 月 14 日
What is your desired output?
Thanx Jonas, I want the last six columns on output containing only the numeric data.
How do you want the * to be handled?

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

 採用された回答

Stephen23
Stephen23 2018 年 8 月 14 日
編集済み: Stephen23 2018 年 8 月 14 日
Here is an efficient solution using textscan. The test file is attached (you did not provide us with a sample file).
opt = {'Delimiter',' +*:','MultipleDelimsAsOne',true,'CollectOutput',true};
fmt = ['%*d%*d%*s%*d%*s',repmat('%f',1,6)];
[fid,msg] = fopen('test.txt','rt');
assert(fid>=3,msg)
C = textscan(fid,fmt,opt{:});
fclose(fid);
Which imports that last six columns of numeric data:
>> C{1}
ans =
0.00000 0.00000 0.00000 0.11000 0.00000 0.88900
0.00000 0.27200 0.00000 0.00600 0.00000 0.72200
0.00000 0.03600 0.00000 0.00000 0.00100 0.96300
0.00000 0.00000 0.00000 0.00000 0.00000 1.00000
0.00000 0.00000 0.00000 0.00000 0.00000 1.00000
0.00000 0.23800 0.36500 0.00900 0.03300 0.35500
0.00000 0.00000 0.00000 0.00000 0.99900 0.00100
0.00000 0.00000 0.00000 0.00000 0.00000 1.00000

4 件のコメント

thanks Stephen Cobeldick, My actual file is attached. I need only the last six columns in this file with only numeric data. Please help me in this regard.
Stephen23
Stephen23 2018 年 8 月 14 日
編集済み: Stephen23 2018 年 8 月 14 日
@Muhammad Shoaib: did you try my answer with your file? My code already works with your file, without any changes. Here are the first and last ten lines from the file, as imported into MATLAB:
>> C{1}(1:10,:)
ans =
0.00000 0.00000 0.00000 0.11000 0.00000 0.88900
0.00000 0.27200 0.00000 0.00600 0.00000 0.72200
0.00000 0.03600 0.00000 0.00000 0.00100 0.96300
0.00000 0.00000 0.00000 0.00000 0.00000 1.00000
0.00000 0.00000 0.00000 0.00000 0.00000 1.00000
0.00000 0.23800 0.36500 0.00900 0.03300 0.35500
0.00000 0.00000 0.00000 0.00000 0.99900 0.00100
0.00000 0.00000 0.00000 0.00000 0.00000 1.00000
0.00000 0.00000 0.00000 0.00000 1.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000 1.00000
>> C{1}(end-9:end,:)
ans =
0.00000 0.00000 0.00000 1.00000 0.00000 0.00000
0.00000 0.00100 0.00000 0.94200 0.05700 0.00000
0.00000 0.26800 0.00000 0.73200 0.00000 0.00000
0.00000 0.00000 0.00000 1.00000 0.00000 0.00000
0.00000 0.00000 0.00000 1.00000 0.00000 0.00000
0.00000 0.00000 0.00000 1.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00400 0.99600 0.00000
0.00000 0.00000 0.00000 1.00000 0.00000 0.00000
0.00000 0.00000 0.00000 1.00000 0.00000 0.00000
thank Stephen Cobeldick, the code worked for me.
jonas
jonas 2018 年 8 月 14 日
Don't forget to accept the answer so that future readers can see that the issue was resolved.

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

その他の回答 (1 件)

jonas
jonas 2018 年 8 月 14 日
編集済み: jonas 2018 年 8 月 14 日
str=fileread('notes.txt');
str=regexprep(str,'[:+*a-zA-Z]',' ')
opt = {'Delimiter',' ','MultipleDelimsAsOne',true,'CollectOutput',true};
out = textscan(str,'%f%f%f%f%f%f%f%f%f',opt{:})

7 件のコメント

Stephen23
Stephen23 2018 年 8 月 14 日
編集済み: Stephen23 2018 年 8 月 14 日
Note that str2num relies on eval, which makes this a risky method to use with input data, especially with data containing weird random characters.
However note that textscan also works on character vectors (not just files), so you could easily use textscan to efficiently parse the char vector into a numeric array.
jonas
jonas 2018 年 8 月 14 日
Oh, didn't know that. Thanks. I like to overuse regexp for everything and figured this could work.
thanks Jones, My actual file is attached. I need only the last six columns in this file with only numeric data. Please help me in this regard.
jonas
jonas 2018 年 8 月 14 日
編集済み: jonas 2018 年 8 月 14 日
Ok, I've updated the solution. This really proved Stephens point, as str2num failed miserably. Just grab the last 6 columns of the results.
I think the other answer worked with your file. Did you even try it?
Thanks Jonas, for your kind support and cooperation.
jonas
jonas 2018 年 8 月 14 日
No problem! I'll refrain from deleting this answer as it shows how to use regexprep to clean up a textfile but make sure to accept the other one :)
Stephen23
Stephen23 2018 年 8 月 14 日
編集済み: Stephen23 2018 年 8 月 14 日
+1 this is a good idea, very useful for complex/unusual file formats. Do not delete!

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

カテゴリ

ヘルプ センター および File ExchangeLanguage Support についてさらに検索

製品

質問済み:

2018 年 8 月 14 日

編集済み:

2018 年 8 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by