0

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.

2
  • 2
    if csvread read 10 value on each row then you should write: meas(j,:) = .... Commented Jun 2, 2016 at 12:36
  • 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. Commented Jun 2, 2016 at 12:49

1 Answer 1

1

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 !

Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately it appears there is a dimension mismatch even when considering your last statement. The post has been editted with your recommendations, thanks.
Obchardon, is there anything more you could suggest?
I also think that your parameter are wrong in csvread: because when you write: csvread(filename,0,0,[row 2 row+j 5]); it means that you get some value on more than one row ! Shouldn't It Be csvread(filename,0,0,[j 2 j 5]);` ? So you read 4 values of only ONE row ! Or csvread(filename,0,0,[row+j 2 row+j 5]);
That is very true! I missed that one, thanks. Still, unfortunately the "Subscripted assignment dimension mismatch" error still persists...
Now your code is correct but I don't know how your data are structured, I cannot help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.