简体   繁体   中英

Writing rows/columns from .csv to matrix using Matlab

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.

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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM