将我的数据从字符数组转换为数组数组
问题描述:
我正在读取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`
答
为了包含由空格分隔的多个数字的字符串转换,你需要使用str2num
而不是str2double
。
str2double
需要单个数字作为字符串或单元格数组的字符串,如果您给它其他任何东西,将返回一个NaN
。
+0
str2num结果的[] –
+0
@EmnaAmeur它可以正常工作与您的示例数据。请发布您的实际文件。 – Suever
如果您直接提供数组中的某些样本而不是图片,测试会发生什么会更容易。你能做到吗? – Roxanne
你好:)完成!我编辑我的问题 –
@EmnaAmeur实际上在您的示例数据(最后)是否反向? – Suever