【发布时间】:2016-06-02 12:26:09
【问题描述】:
使用 .csv 文件,我试图从 .csv 文件中读取一组行,然后将每一行写入一个矩阵。
发生错误,指出:
'Index exceeds matrix dimensions.'
Error in Id_Vg (line 17)
meas(j,:) = csvread(filename,0,0,[row 2 row+j 5]);
此行试图将第 2 到 10 列写入“meas”的一个元素是否存在问题?
我认为用零预分配“meas”将允许将每个单独的 .csv 列和行写入“meas”。
function [ output_args ] = Id_Vg(filename, row,...
nopoints, noskip);
%directory= address of the .csv files "C:\....."
%filename = .csv name "1us.csv"
%row = starting row to ignore all text before values, "120"
%nopoints = number of measurement points at each condition "[1e1 1e3 1e1 1e3 1e2]"
%noskip = determines whether to skip condition "[0 1 0 1 0]" 0=skip.
rowstop = row+sum(nopoints);
M = csvread(filename,row,1,[row 2 rowstop 5]);
meas = zeros(sum(nopoints),4);
for i = 1:length(nopoints)
if noskip(i) == 0
for j=1:sum(nopoints)
meas(j,:) = csvread(filename,0,0,[row 2 row+j 5]);
end
end
end
编辑:将“meas(j)”更改为“meas(j,:)”并更改了有用的 .csv 列,因此不会将空列写入“meas”。
错误依旧:
Subscripted assignment dimension mismatch.
我知道这通常是由于在这种情况下,将太多 .csv 列写入没有足够列的矩阵。情况并非如此,因为即使向 meas 写入更多数量的零也会产生相同的错误。
【问题讨论】:
-
如果 csvread 在每行读取 10 个值,那么您应该写:
meas(j,:) = ...。 -
发生同样的错误,但谢谢。这应该有助于将 .csv 列写入“meas”。可能这与我在原始评论中提到的问题不同。