简体   繁体   中英

Octave : I receive a dimension error: cat: dimension mismatch when doing cell2mat

I have a cell array (class cell) with dimension that I try to convert to doubles. I get a dimension error when I use cell2mat. (row 1 and kol 1 are not numeric)

debug> `class(mycsvdata)`
ans = cell

CCC=cell2mat(mycsvdata(2:end,2:end))
error: cat: dimension mismatch
error: called from
cell2mat at line 80 column 11
leesCsvPuur at line 7 column 2
verwerkStooq at line 37 column 8
handleStooq at line 77 column 1
testinvoer03 at line 72 column 1

debug> `size(mycsvdata)`
ans =
   9   7

I tried cell2mat :

debug> `cell2mat{1 2 3; 4 5 6}`
ans =
   1   2   3
   4   5   6

I would appreciate any advice.

There's a lot of information missing here, but I suspect your problem boils down to the following three facts.

  1. A standard array must necessarily always contain elements of the same type . It cannot, eg contain a 'string' in one index, and a 'number' in another; that's what cell arrays are for.

  2. Attempting to convert a cell array which contains such mixed elements into a standard array via cell2mat should, depending on your warning / error levels, either fail, or convert all elements to their least common denominator. Eg if you have both 'date strings' (like '2011-01-02') and 'numbers', it will probably interpet the 'numbers' as ascii character codes first.

  3. Strings in octave are simply arrays of characters. If you try to vertically concatenate two strings which are of unequal length, you will get an error about mismatched dimensions, since the resulting array in a concatenation always needs to be properly rectangular.

Eg if all strings are same size (note numeric conversion)

> c = { '2011-01-01', 98.1; '2011-01-20', 97 };
> C = cell2mat(c)
warning: implicit conversion from numeric to char
  C =
    2011-01-01b
    2011-01-20a

If they're not (note the unpadded date):

> c = { '2011-01-01'; '2011-01-2' };
> C = cell2mat(c)
error: cat: dimension mismatch
error: called from
    cell2mat at line 80 column 11

I don't know the exact nature of the cell array resulting from your csv2cell operation, but I would wager something similar to the above is going on.

In general, it is probably not the right approach to convert via cell2mat immediately. Instead, you could probably collect all columns to separate variables, and then treat them accordingly, either via cell2mat or via a cellfun approach.

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