我在 .xlsx
中有积分数据我想读取它们并将它们存储在p
Matlab 中的数组。这些点只是 x
的 3D 坐标, y
, z
这样已经理解了三列而不是prdfined行。如何从 .xlsx
中检索它们或 .csv
如果我在尝试检索时需要快速检索 .xlsx
并且它的响应时间很慢并且返回一个空数组。可能以转置形式存储它们并将其转置回来。
我的代码:.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
到达的声明之间没有空格) .因此,对于问题中的给定数据,输入 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
关于excel - 在 Matlab 中检索 Xlsx 或 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23444551/