You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Convert a .txt file into .mat fiie in MATLAB.
5 views (last 30 days)
Show older comments
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
Answers (1)
Ameer Hamza
on 7 Nov 2020
Edited: Ameer Hamza
on 7 Nov 2020
Try something like this
str = fileread('data.txt');
data = textscan(str, '%x')
data.txt used in my code is attached.
21 Comments
Dhruv Bhatnagar
on 7 Nov 2020
@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
on 7 Nov 2020
Edited: Ameer Hamza
on 7 Nov 2020
Are you looking for something like this?
str = strsplit(fileread('data.txt'), '\n');
data = cellfun(@(x) textscan(x, '%1x'), str)
Dhruv Bhatnagar
on 7 Nov 2020
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
on 8 Nov 2020
But you only have 15 characters in 2nd line. In MATLAB, to create a matrix, all rows must have an equal length.
Dhruv Bhatnagar
on 8 Nov 2020
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
on 8 Nov 2020
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
on 8 Nov 2020
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
on 8 Nov 2020
Which MATLAB release are you using?
Dhruv Bhatnagar
on 8 Nov 2020
I am using R2017b version.
Ameer Hamza
on 8 Nov 2020
Try this code. This should work in R2017b
str = replace(fileread('data.txt'), {newline ' '}, '');
data = reshape(hex2dec(str(:)), 16, []).'
Dhruv Bhatnagar
on 9 Nov 2020
Thank you very much . It works
Ameer Hamza
on 9 Nov 2020
What is the difference?
Dhruv Bhatnagar
on 9 Nov 2020
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
on 9 Nov 2020
You can directly use writematrix() instead of writing a for-loop.
Dhruv Bhatnagar
on 9 Nov 2020
Thank you soo much it works
Ameer Hamza
on 10 Nov 2020
I am glad to be of help!!!
Dhruv Bhatnagar
on 10 Nov 2020
Thank you sir
Ameer Hamza
on 11 Nov 2020
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
on 11 Nov 2020
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);
Dhruv Bhatnagar
on 11 Nov 2020
Thank You Sir
Ameer Hamza
on 11 Nov 2020
I am glad to be of help!!!
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)