poster
2019-12-10, 16:49
我正在尝试使用load()将以下ascii文件加载到MATLAB中
% some comment 1 0xc661 2 0xd661 3 0xe661 (这实际上是一个简化的文件。我尝试加载的实际文件在开始时包含未定义的列数和未定义的注释行数,这就是为什么加载功能很吸引人的原因)
由于某些奇怪的原因,我得到以下信息:
K>> data = load('testMixed.txt') data = 1 50785 2 58977 3 58977 我观察到,只要十六进制数字中有一个“ d”,就会出现此问题。
直接hex2dec转换正常工作:
K>> hex2dec('d661') ans = 54881 importdata似乎有相同的转换问题,ImportWizard也是如此:
K>> importdata('testMixed.txt') ans = 1 50785 2 58977 3 58977 那是一个错误,我是以某种禁止的方式使用加载功能,还是我明显忽略了某些东西?
是否有解决此问题的方法,而不必自己重新实现文件解析?
编辑我的输入文件以更好地反映我的实际文件格式。我对最初的问题有些过分简化。
回答:
“高尔夫”答案:
这从mtrw (https://stackoverflow.com/questions/1539343/problem-bug-loading-hexadecimal-data-into-matlab/1540031#1540031)的答案开始,并进一步缩短:
fid = fopen('testMixed.txt','rt'); data = textscan(fid,'%s','Delimiter','\n','MultipleDelimsAsOne','1',... 'CommentStyle','%'); fclose(fid); data = strcat(data{1},{' '}); data = sscanf([data{:}],'%i',[sum(isspace(data{1})) inf]).'; 上一个答案:
我的第一个想法是使用TEXTSCAN (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/textscan.html) ,因为它有一个选项,当某些行以给定字符(例如% )开头时,您可以忽略它们。但是,TEXTSCAN似乎不能很好地处理十六进制格式的数字。这是另一个选择:
fid = fopen('testMixed.txt','r'); % Open file % First, read all the comment lines (lines that start with '%'): comments = {}; position = 0; nextLine = fgetl(fid); % Read the first line while strcmp(nextLine(1),'%') comments = [comments; {nextLine}]; % Collect the comments position = ftell(fid); % Get the file pointer position nextLine = fgetl(fid); % Read the next line end fseek(fid,position,-1); % Rewind to beginning of last line read % Read numerical data: nCol = sum(isspace(nextLine))+1; % Get the number of columns data = fscanf(fid,'%i',[nCol inf]).'; % Note '%i' works for all integer formats fclose(fid); % Close file 这将在文件开头处理任意数量的注释。求列数的计算是受Jacob答案的 (https://stackoverflow.com/questions/1539343/problem-bug-loading-hexadecimal-data-into-matlab/1539410#1539410)启发。
更多&回答... (https://stackoverflow.com/questions/1539343)
% some comment 1 0xc661 2 0xd661 3 0xe661 (这实际上是一个简化的文件。我尝试加载的实际文件在开始时包含未定义的列数和未定义的注释行数,这就是为什么加载功能很吸引人的原因)
由于某些奇怪的原因,我得到以下信息:
K>> data = load('testMixed.txt') data = 1 50785 2 58977 3 58977 我观察到,只要十六进制数字中有一个“ d”,就会出现此问题。
直接hex2dec转换正常工作:
K>> hex2dec('d661') ans = 54881 importdata似乎有相同的转换问题,ImportWizard也是如此:
K>> importdata('testMixed.txt') ans = 1 50785 2 58977 3 58977 那是一个错误,我是以某种禁止的方式使用加载功能,还是我明显忽略了某些东西?
是否有解决此问题的方法,而不必自己重新实现文件解析?
编辑我的输入文件以更好地反映我的实际文件格式。我对最初的问题有些过分简化。
回答:
“高尔夫”答案:
这从mtrw (https://stackoverflow.com/questions/1539343/problem-bug-loading-hexadecimal-data-into-matlab/1540031#1540031)的答案开始,并进一步缩短:
fid = fopen('testMixed.txt','rt'); data = textscan(fid,'%s','Delimiter','\n','MultipleDelimsAsOne','1',... 'CommentStyle','%'); fclose(fid); data = strcat(data{1},{' '}); data = sscanf([data{:}],'%i',[sum(isspace(data{1})) inf]).'; 上一个答案:
我的第一个想法是使用TEXTSCAN (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/textscan.html) ,因为它有一个选项,当某些行以给定字符(例如% )开头时,您可以忽略它们。但是,TEXTSCAN似乎不能很好地处理十六进制格式的数字。这是另一个选择:
fid = fopen('testMixed.txt','r'); % Open file % First, read all the comment lines (lines that start with '%'): comments = {}; position = 0; nextLine = fgetl(fid); % Read the first line while strcmp(nextLine(1),'%') comments = [comments; {nextLine}]; % Collect the comments position = ftell(fid); % Get the file pointer position nextLine = fgetl(fid); % Read the next line end fseek(fid,position,-1); % Rewind to beginning of last line read % Read numerical data: nCol = sum(isspace(nextLine))+1; % Get the number of columns data = fscanf(fid,'%i',[nCol inf]).'; % Note '%i' works for all integer formats fclose(fid); % Close file 这将在文件开头处理任意数量的注释。求列数的计算是受Jacob答案的 (https://stackoverflow.com/questions/1539343/problem-bug-loading-hexadecimal-data-into-matlab/1539410#1539410)启发。
更多&回答... (https://stackoverflow.com/questions/1539343)