0
votes

Using a .csv file, I am attempting to read a set of rows from a .csv file and then write each row to a matrix.

An error occurs that states:

 'Index exceeds matrix dimensions.'
 Error in Id_Vg (line 17)
        meas(j,:) = csvread(filename,0,0,[row 2 row+j 5]);

Is the issue that this line is attempting to write columns 2 to 10 into one element of 'meas'?

I thought that preallocating 'meas' with zeros would allow each individual .csv column and row to be written to '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

Edit: Changed 'meas(j)' to 'meas(j,:)' and altered the useful .csv columns so that no empty columns are written to 'meas'.

Error still occurs:

Subscripted assignment dimension mismatch.

I understand this is usually due to, in this case, writing too many .csv columns to a matrix that does not have sufficient columns. This should not be the case as even writing a higher number of zeros to meas produces the same error.

1
if csvread read 10 value on each row then you should write: meas(j,:) = ....obchardon
The same error occurs but thanks. This should help with writing the .csv columns to 'meas'. Possibly it's a different issue to what I mentioned in the original comment.Azaxa

1 Answers

1
votes

You're right: the issue is that this line is attempting to write columns 2 to 10 into one element of 'meas'.

You should write meas(j,:) = ... instead of meas(j) = ...

meas(j,:) is equivalent to meas(1,1:10).

if you write meas(j) = [1 2], you're trying to write 2 element into one element of meas which is impossible.

And be carefull with this code your csvread should always return 10 values !