我正在读取一个plt文件,但是我发现将数据从字符数组转换为数字数组时出现问题。我尝试使用str2num和str2double,reulst总是:[]或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`
答案 0 :(得分:0)
为了转换包含由空格分隔的多个数字的字符串,您需要使用str2num
而不是str2double
。
str2double
期望将单个数字作为字符串或字符串的单元格数组,如果您将其提供给其他任何内容,则会返回NaN
。