readtable() interpreting HEX as scientific

조회 수: 22 (최근 30일)
Michael Bernico
Michael Bernico 2020년 3월 11일
댓글: Guillaume 2020년 3월 19일
I have a script that is importing a csv file that contains decimal and hexadecimal values. Currently I am only utilizing the HEX values. The issue I have is occassionally the data has a value of something like 6E60 which matlab interprets as scientific and then errors on the hex2dec function. Is there a function that would suit better to avoid this issue or is there a format setting which I can apply prior to the readtable function to interpret the csv data as text?
Here are the relevant lines:
clear
clc
format short
warning('OFF', 'MATLAB:table:ModifiedVarnames')
[baseName, folder] = uigetfile({'*.csv';'*.xls';'*.xlsx';'*.txt'},'Select File for Table Data');
file = fullfile(folder, baseName);
The resulting variable is used to create a dataset from a search function
DataSet=csvData(r,:);
avgval=3.3*mean(hex2dec(DataSet.measurement))/65536;
avgval is the line that has issues.
  댓글 수: 1
dpb
dpb 2020년 3월 11일
"Here are the relevant lines:"
Excepting for the only one, truly relevant line is the one that was used to actually read the file...

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

채택된 답변

Guillaume
Guillaume 2020년 3월 11일
In recent versions of matlab, the easiest would be to use detectImportOptions and override the relevant variable type to force it to char. However, R2015a predates detectImportOptions (introduced in R2016b). The only way for you to override the automatic type detection is to specify the 'Format' property in the readtable call. Unfortunately, that means you have to specify the format of all columns and know the exact number of columns rather than letting readtable work that out for you.
It would be something like:
data = readtable('yourfile', 'Format', '%f%f%s%s%f'); %for a file with 6 columns
where '%s' forces the corresponding column to be read as text regardless of its content.
  댓글 수: 7
dpb
dpb 2020년 3월 11일
"OP did fill the matlab version on the right -> (below the tags)."
Huh. Took me about a minute to find it even with the pointer... :) I never look outside the text of the Q? and/or Comments so whiffed on that entirely.
Guillaume
Guillaume 2020년 3월 19일
"It's a glaring weakness imo that textscan, readtable cannot deal with hex input data (nor complex), only fscanf can handle hex; C doesn't know anything about complex."
Now that 2020a is out, you'll be pleased to know that this glaring weakness has finally been fixed.
textscan now has '%x' and '%b' format specifiers, readtable automatically recognises the 0x and 0b prefixes and there's various options for detectImportOptions to support direct conversion from hexadecimal or binary (with or without prefixes).
Hooray!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by