MatLab doesn't read my .TXT file correct

조회 수: 65 (최근 30일)
Peter Bu
Peter Bu 2018년 7월 20일
댓글: Noam Greenboim 2020년 5월 21일
Hello everyone,
i have an issue where my .TXT file is not readable my MatLab. I am not sure what to do. Can somebody please give me a hint? I can open it with other tools like "Atom".
Thanks in advance!
  댓글 수: 2
Jeremy Hughes
Jeremy Hughes 2018년 7월 20일
It would help to see the actual file. My suspicion is that the file is encoded with something MATLAB doesn't expect, like UTF-16.
Peter Bu
Peter Bu 2018년 7월 20일
Oh yes. I missed to attach the file. Here it is :-)

댓글을 달려면 로그인하십시오.

채택된 답변

Jeremy Hughes
Jeremy Hughes 2018년 7월 20일
Thanks for uploading the file!
The first issue I see is that the file has a UTF-16 byte-order-mark. The file is stored as UTF-16 which isn't fully supported in MATLAB. This is why you see the extra boxes between each of the characters.
I did the following to check that your file even needed to be UTF-16--it doesn't.
fid = fopen('Charge4_Probe_3.txt','r','n')
bytes = fread(fid)';
fclose(fid);
bytes(1:20) % inspect a few of the bytes, you can see what's going on
bytes(1:2) = []; % remove the byte order mark
any(bytes(2:2:end) ~= 0) % every other byte is NULL
So none of the characters are using the second byte--effectively this is an ascii file. I'd look into the source of the file and see if you can get it as utf-8 (that will half the file size)
To get a new file, you can write as follows:
asciibytes = bytes(1:2:end) % strip out the zero bytes
fid = fopen('Charge4_Probe_3_ascii.txt','w','n','UTF-8');
fwrite(fid,asciibytes);
fclose(fid);
This is now ready to be read. (Note: as Aquatris mentions, these are using a decimal separator of comma.)
To read this programatically, you can do this:
opts = detectImportOptions('Charge4_Probe_3_ascii.txt','Delimiter','\t');
opts.DataLines = [4,inf];
opts = setvartype(opts,'double');
opts = setvaropts(opts,'DecimalSeparator',',');
T = readtable('Charge4_Probe_3_ascii.txt',opts);
head(T)
Hope this helps,
Jeremy
  댓글 수: 2
Peter Bu
Peter Bu 2018년 7월 20일
Thanks a lot for your time and the detailed answer!
Noam Greenboim
Noam Greenboim 2020년 5월 21일
Here is an easy way to tell the encoding of your file:
Usage:
FileEncoding = BOM('Charge4_Probe_3.txt')
This uses the same idea as described above.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Aquatris
Aquatris 2018년 7월 20일
Your data has comma (,) in them. I do not think Matlab can read values with commas, i.e., 2,230. You should change those commas to dots as in answered in another question in this forum .
  댓글 수: 2
Aquatris
Aquatris 2018년 7월 20일
편집: Aquatris 2018년 7월 20일
Also, I used Matlabs "import data" functionality and it was able to read the data correctly, just the variables with commas were NaN instead.
Peter Bu
Peter Bu 2018년 7월 20일
also thank you for your time and answer :-)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by