首页 > 解决方案 > 将矩阵导出到 Microsoft Access。使用数据库/插入时出错

问题描述

你好! 以下是以下任务的代码:

(!)困难在于只有矩阵的第一行被写入 Microsoft Access(三个可用的),然后显示错误消息:

(?)请告诉我,为了在 Microsoft Access 中记录矩阵的所有三行,有必要添加/更正代码吗?

Q=[];
A={1, [3 5 8]} % array of cells

for j=1:2 % matrix column index
for i=1:3 % matrix row index
if j==1
Q(i,j)=A{1,j};
else 
Q(i,j)=A{1,j}(1,i);
end
end
end

Q

conn = database('QWERT', '', '');
colnames = {'u1', 'u2'};

insert(conn, 'Rtu', colnames, Q);
close(conn);

在此处输入图像描述

标签: databasematlabms-accessmatrixexport

解决方案


我不使用 matlab,但正常的编程实践会建议将插入插入到您的外部 for 循环中:这可行吗?

Q=[];
A={1, [3 5 8]} % array of cells
conn = database('QWERT', '', '');
colnames = {'u1', 'u2'};

for j=1:2 % matrix column index
  for i=1:3 % matrix row index
    if j==1
      Q(i,j)=A{1,j};
    else 
      Q(i,j)=A{1,j}(1,i);
    end
  end
  insert(conn, 'Rtu', colnames, Q);
end

Q

close(conn);

或者在插入周围放置另一个 for 循环...


推荐阅读