Xlsx或csv在Matlab中回顾
我有.xlsx
点数据,我想在Matlab中读取并存储在p
数组中。 这些点只是x
, y
, z
三维坐标 ,从而理解了三列而不是三列。 如果我需要快速检索,我如何能从.xlsx
或.csv
,因为我试图检索.xlsx
,其响应时间很慢,并返回一个空数组。 可能将它们以转置的forms存储并转换回来。
我的代码:.Xls阅读
A = xlsread('data.xlsx')
输出:
A = []
我的代码:.CSV阅读
M = csvread('data.csv')
输出:
Error using dlmread (line 139) Mismatch between file and format string. Trouble reading number from file (row 2u, field 1u) ==> ;\n Error in csvread (line 48) m=dlmread(filename, ',', r, c);
分数设置1:
-191.2442 187.7193 1.0000; -155.2995 152.6316 2.0000; -182.0276 104.6784 3.0000; -148.8479 84.7953 4.0000;
点数2:
-142.3963 83.6257 5.0000; -102.7650 133.9181 6.0000; -56.6820 164.3275 7.0000; -30.8756 124.5614 8.0000; -23.5023 118.7135 7.0000; -9.6774 110.5263 6.0000; 26.2673 90.6433 5.0000; -42.8571 -6.4327 4.0000; 10.5991 7.6023 3.0000;
点集3:
-73.2719 84.7953 9.0000; -137.7880 15.7895 10.0000; -92.6267 -30.9942 9.0000; -42.8571 19.2982 8.0000; 41.0138 -15.7895 4.0000; 71.4286 -41.5205 6.0000; 90.7834 -14.6199 5.0000;
看看这个稍微扭曲一个使用importdata
适合你 –
C1 = importdata(file1) %%// file1 is your CSV filename t1 = regexp(C1,'\s','Split') t2 = horzcat(t1{:}) t2 = strrep(t2,';','') M = cellfun(@str2num,reshape(t2(~strcmp(t2,'')),3,[])')
编辑1:这种情况下,假定您有一个CSV
文件,它将所有的Point Sets
聚集在一起,但是一个接一个(在Point Sets
和它们的数据之间没有空格,也在一个Point Set
的终点和到达下一个Point Set
) 。
所以,input的CSV文件看起来像这个问题给定的数据 –
Points Set 1: -191.2442 187.7193 1.0000; -155.2995 152.6316 2.0000; -182.0276 104.6784 3.0000; -148.8479 84.7953 4.0000; Points Set 2: -142.3963 83.6257 5.0000; -102.7650 133.9181 6.0000; -56.6820 164.3275 7.0000; -30.8756 124.5614 8.0000; -23.5023 118.7135 7.0000; -9.6774 110.5263 6.0000; 26.2673 90.6433 5.0000; -42.8571 -6.4327 4.0000; 10.5991 7.6023 3.0000; Points Set 3: -73.2719 84.7953 9.0000; -137.7880 15.7895 10.0000; -92.6267 -30.9942 9.0000; -42.8571 19.2982 8.0000; 41.0138 -15.7895 4.0000; 71.4286 -41.5205 6.0000; 90.7834 -14.6199 5.0000;
请注意,代码的结果将是一个数组的结构。
码
C1 = importdata(file1) %%// file1 is your CSV filename ind1 = cellfun(@isempty,strfind(C1,'Points')) start_ind = find(~ind1)+1 s1 = find(~ind1)-1; stop_ind = [s1(2:end) ; numel(ind1)] for k = 1:numel(start_ind) data1 = C1(start_ind(k):stop_ind(k)) t1 = regexp(data1,'\s','Split') t2 = strrep(horzcat(t1{:}),';','') t2 = t2(~strcmp(t2,'')) array(k).data = cellfun(@str2num,reshape(t2,3,[])'); %%//' end
您正在使用行分隔符;\n
(分号+新行)。 我不知道有什么function理解这种格式。 使用texstscan
可能是最好的select:
fid=fopen(...) M=cell2mat(textscan(line,'%f,%f,%f;\n')); fclose(fid);