MATLAB Answers

How to read 16-bit text with Matlab

177 ビュー (過去 30 日間)
Frank
Frank 2016 年 3 月 1 日
コメント済み: J. Alex Lee 2020 年 3 月 23 日
I have an Agilent 34970A data logger with their BenchLink software. This puts out a .csv file that Excel reads with no trouble. But Matlab cannot import it because it's 16 bit text. The first two bytes are 0xFF 0xFE, then after that every text byte is followed by a NULL (0x00). I wrote a function to read it, but I was hoping for the ability to read it directly, without having to programmatically skip the NULLs.

  0 件のコメント

サインイン to comment.

採用された回答

Walter Roberson
Walter Roberson 2016 年 3 月 1 日
MATLAB does not handle UTF-16LE files. You might wish to use the source code I posted in http://uk.mathworks.com/matlabcentral/answers/267176-read-and-seperate-csv-data#answer_209938 which reads csv files from any of the UTF-8 / UTF-16 / UTF-32 encodings

  7 件のコメント

表示 4 件の古いコメント
J. Alex Lee
J. Alex Lee 2020 年 3 月 21 日
If you came back here from Walter's answer to the other question and are confused, unhide the older comments!
So just to double check, the number of bytes to skip is the BOM_size, right? So, the final solution would be
[encoding,bytes_per_char,BOM_size] = detect_UTF_encoding(srcFile);
fidr = fopen(srcPath, 'rt', 'n', encoding);
% skip
fread(fidr, BOM_size, '*uint8');
filecontent = fread(fidr, [1 inf], '*char');
And for clarification, the BOM_size is in bytes, right? If so is it clearer/better to use fseek to skip?
[encoding,bytes_per_char,BOM_size] = detect_UTF_encoding(srcFile);
fidr = fopen(srcPath, 'rt', 'n', encoding);
% skip
fseek(fidr, BOM_size, 0);
filecontent = fread(fidr, [1 inf], '*char');
Walter Roberson
Walter Roberson 2020 年 3 月 21 日
Yes BOM_size is bytes.
fseek() has more overhead than fread and discarding at that point. fseek() has to execute code to check for buffered output and wait for it to be completed, and reset the end of file flag, and re-buffer from the input file because fseek is the system call used to synchronize between multiple process i/o.
J. Alex Lee
J. Alex Lee 2020 年 3 月 23 日
deep...thanks!

サインイン to comment.

その他の回答 (0 件)

サインイン してこの質問に回答します。


Translated by