how to read a textfile that contain an array of text values into matrix
조회 수: 6 (최근 30일)
이전 댓글 표시
I have a textfile that contain tab delimited hexadecimal values and I would like to transfer the values into an array with each element will keep a value in the table (attached)
댓글 수: 0
답변 (4개)
Massimo Zanetti
2017년 3월 16일
편집: Massimo Zanetti
2017년 3월 16일
Here is it, you should know how many columns your data span (I guessed by reading your file they are 25):
%open file
fname = 'PowerUpRampSpeed_1.txt';
fileID = fopen(fname);
%read formatted data as strings of 2 characters
A = textscan(fileID,'%2c','Delimiter','\t');
%reshape the vector to matrix size
cols = 25;
A = reshape( hex2dec(char(A)) , cols , [])';
%close file
fclose(fname);
Now, A is your matrix read into Matlab.
댓글 수: 0
Yoav Weizman
2017년 3월 20일
편집: per isakson
2017년 6월 2일
댓글 수: 3
Walter Roberson
2017년 6월 2일
Your solution leaves the values as text. Were you wanting a text array, or were you wanting a numeric array?
Walter Roberson
2017년 6월 2일
Oddly, at least on OS-X, textread() is not able to read files with the original name PowerUpRampSpeed_1µs.txt claiming they do not exist . I will be filing a bug report on that.
Walter Roberson
2017년 6월 2일
S = fileread('PowerUpRampSpeed_1µs.txt');
temp = regexp( regexp(S, '\r?\n', 'split'), '\W+', 'split');
output = vertcat(temp{:});
The result will be a 32 x 200 cell array of character vectors; hex values will not be converted to numeric.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!