Convert a .txt file into .mat fiie in MATLAB.
4 次查看(过去 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!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 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 (한국어)
