Excel sheet data upload "Invalid 'DataRange'. The column size must match the number of variables".

12 views (last 30 days)
"Invalid 'DataRange'. The column size must match the number of variables".
I imported xlsx (excel) data into matlab then generated a script after which I copied into the beginning of a different script i wrote but I get the above error. Can you help me fix this? Why do I get this error?

Accepted Answer

Walter Roberson
Walter Roberson on 26 Jan 2023
opts.DataRange = "A2:AT78";
That requests that column "A" to "AT" be read in. However, M1_New_Macro1.xlsx only has data up to column "AS"
The headers in the xlsx file do not align with the content of the data. Observe that column A is titled "cut" and contains a file name, and column B is titled "Count" and contains integer numbers (plausibly counts of something.) Then look at column L "Slice" which is empty, then column M which named "Count" but contains file names. The Total Area in column C contains fractions but the Total Area in column N contains only integers (looking like a count)
It looks to me as if there are blank columns in the data but that the headers in row 1 do not take that into account.
When you look at the variable named in the code, it is not obvious to me that the variable names match up to the data in the file.
Starting in row 34 the file contains additional tables that are not in the same format as the data above that.
You should probably be using readcell() instead of readtable(), and extracting the data you need from the resulting cells.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by