-3

我正在读取一个 plt 文件,但我发现将数据从字符数组转换为数值数组时出现问题。我尝试使用 str2num 和 str2double,结果总是:[] 或 NaN。

[filename pathname] = uigetfile({'*.plt'},'File Selector');
fullpathname = strcat(pathname,filename);
set(handles.text2,'string',fullpathname);%show full path name
loaddata = fullfile(pathname,filename);
fid = fopen(loaddata, 'r');% Read the entire file into memory
contents = fread(fid,'*char')';
fclose(fid);
contents = strrep(contents, ',', '.')% Replace `,` with `.`
data = str2double(contents)% Now convert to numbers`

你在这里找到一些来自内容的样本:

+8.7595000000000000E+03      +0.0000000000000000E+00    
+0.0000000000000000E+00      +8.3581639938152974E-01   
+0.0000000000000000E+00      +4.6014153848539308E+01  
+4.6014153852568526E+01      +0.0000000000000000E+00  
+0.0000000000000000E+00      +8.3581639938152974E-01  
+0.0000000000000000E+00      +2.2902134241670819E+01` 
4

1 回答 1

0

为了转换包含由空格分隔的多个数字的字符串,您需要使用str2numand not str2double

str2double期望将单个数字作为字符串或字符串元胞数组,如果您提供其他任何内容,将返回一个NaN.

于 2016-11-23T13:30:45.793 回答