이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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 (한국어)
