excel - 在 Matlab 中检索 Xlsx 或 csv

标签 excel matlab csv

我在 .xlsx 中有积分数据我想读取它们并将它们存储在p Matlab 中的数组。这些点只是 x 的 3D 坐标, y , z这样已经理解了三列而不是prdfined行。如何从 .xlsx 中检索它们或 .csv如果我在尝试检索时需要快速检索 .xlsx并且它的响应时间很慢并且返回一个空数组。可能以转置形式存储它们并将其转置回来。
Scren Shot of my excel file

我的代码:.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/

相关文章:

matlab - 从返回结构体的函数返回一个字段

javascript - 用jquery导出的csv文件中的行返回字符的来源?

VB.NET:将 CSV 文件读入二维数组

python - "Incorrect number of bindings supplied"cPython 3.5 SQLite3 VS15

excel - 复制由空白单元格组成的列数据

excel - 我的代码出现 "Permission Denied"错误

excel - 提取单元格内的特定值(正则表达式模式)

vba - Excel 立即窗口打印公式值

matlab - 查找稀疏矩阵中的行最小值

python - R - 通过删除 x、y 和 z 维度的全零二维矩阵来调整 3D 数组的大小