現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Convert a .txt file into .mat fiie in MATLAB.
5 ビュー (過去 30 日間)
古いコメントを表示
Dear Members I am new to MATLAB.
I have one .txt file containg information in hex format as given below:
1234ABC6789D
AB456C78DEF
......
Like this almost 1000 values are there.
I want to write it in matrix format in matlab.
So can someone please explain how to convert this .txt file into .mat file
回答 (1 件)
Ameer Hamza
2020 年 11 月 7 日
編集済み: Ameer Hamza
2020 年 11 月 7 日
Try something like this
str = fileread('data.txt');
data = textscan(str, '%x')
data.txt used in my code is attached.
21 件のコメント
Dhruv Bhatnagar
2020 年 11 月 7 日
@Ameer Hamza Thanks for your reply. But it is giving string file in one cell. I want in each cell one one value.
Example: If my txt value contain first value as 89CD7632EFAB1054
Output Required:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
8 9 12 13 7 6 3 2 14 15 10 11 1 0 5 4
Ameer Hamza
2020 年 11 月 7 日
編集済み: Ameer Hamza
2020 年 11 月 7 日
Are you looking for something like this?
str = strsplit(fileread('data.txt'), '\n');
data = cellfun(@(x) textscan(x, '%1x'), str)
Dhruv Bhatnagar
2020 年 11 月 7 日
Thanks for your quick reply sir
But it is giving error and output is also coming in two cells only like this
1 2
1234ABC6789D AB456C78DEF
I want every bit in each cell such that it would be 2row 16 columns came 2x12 .mat file.
Ex: I want output in this format:

Ameer Hamza
2020 年 11 月 8 日
But you only have 15 characters in 2nd line. In MATLAB, to create a matrix, all rows must have an equal length.
Dhruv Bhatnagar
2020 年 11 月 8 日
No no you took me wrong . Sorry its my mistake I didnot paid attention to your input length.
All rows are of equal length from 1 to 16 bit long like this:
89CD7632EFAB1054
64135720B9CE8AFD
059C36AF72EB41D8
79B568A43DF12CE0
9C638D7250AF41BE
16CB34E9528F70AD
B649E31CD02F857A
906FB24D7E815CA3
9A03DE4721B865FC
17536024BDF9CA8E
Ameer Hamza
2020 年 11 月 8 日
In that case try this
str = fileread('data.txt');
data = textscan(str, '%1x');
data = reshape(data{1}, 16, []).'
Result
>> data
data =
10×16 uint64 matrix
8 9 12 13 7 6 3 2 14 15 10 11 1 0 5 4
6 4 1 3 5 7 2 0 11 9 12 14 8 10 15 13
0 5 9 12 3 6 10 15 7 2 14 11 4 1 13 8
7 9 11 5 6 8 10 4 3 13 15 1 2 12 14 0
9 12 6 3 8 13 7 2 5 0 10 15 4 1 11 14
1 6 12 11 3 4 14 9 5 2 8 15 7 0 10 13
11 6 4 9 14 3 1 12 13 0 2 15 8 5 7 10
9 0 6 15 11 2 4 13 7 14 8 1 5 12 10 3
9 10 0 3 13 14 4 7 2 1 11 8 6 5 15 12
1 7 5 3 6 0 2 4 11 13 15 9 12 10 8 14
Ameer Hamza
2020 年 11 月 8 日
Dhruv Bhatnagar's comment posted as answer moved here:
This error is coming:
Error using textscan
Unable to parse the format character vector at position 1 ==> %1x
Unsupported format specifier '%x'. See the documentation for TEXTSCAN for supported formats.
Error in trial_1 (line 2)
data = textscan(str, '%1x');
Ameer Hamza
2020 年 11 月 8 日
Try this code. This should work in R2017b
str = replace(fileread('data.txt'), {newline ' '}, '');
data = reshape(hex2dec(str(:)), 16, []).'
Dhruv Bhatnagar
2020 年 11 月 9 日
Actually whatever output I am getting of 1000x16 matrix I want to save it that in one text file.
How to do that.
Ameer Hamza
2020 年 11 月 11 日
Dhruv's answer moved here
using writematrix output is saving in below format in text file:
1,5,6,13,15,12,3,2,1,0,11,10,7,8,9
But I want to save the output in text file in this format:
1 5 6 D F C 3 2 1 0 B 10 7 8 9
How can I modify this?
Ameer Hamza
2020 年 11 月 11 日
You can do it like this
x = [1,5,6,13,15,12,3,2,1,0,11,10,7,8,9];
f = fopen('data.txt', 'w');
fprintf(f, '%x ', x);
fclose(f);
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
