readcell command doesn't work anymore!

Hi all,
I have a matlab code that reads data from an excel sheet using the readcell function.
I have a data starting in cell B3 to Bx (x can be different).
So I used the following finction to get these data
x1 = readcell("model","sheet","sheet name","Range",'B3:B3');
This function used to give me the needed results (all non empty cells in col B starting from B3). However, I just tried to run my code again today and I was chocked that this command doesn't work anymore!!! error !!!
any idea? please help me!
Thanks in advance,
Lara

4 Comments

@Lara QASIM : please show us the complete error message. This means all of the red text.
What is the full and exact text of the error message you received? Include all the text displayed in red in the Command Window.
This is the error:
and the error message i got is :
Index in position 1 exceeds array bounds (must not exceed 1).
Error in orient (line 35)
FCM_f(j).Function=char(x1(j,1));
This is part of the code where the error occurs:
Size_FCM = size(FCM);
x1 = readcell("model","sheet","Global FCM","Range",'B3:B3');
for j= 1:Size_FCM(1,1)
FCM_f(j).value=FCM(j,:);
FCM_f(j).Function=char(x1(j,1));
end
When I checked x1 .. itonly has the value from cell B3 and not all the col.
That's when when j=2 I have an error..
but the question is why this function used to work and just suddenly stopped working?

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 23 Mar 2020
Edited: Stephen23 on 23 Mar 2020
This only reads one cell:
"Range",'B3:B3'
There is nothing in your code that reads "all non empty cells in col B starting from B3" as you write. It just reads B3.

6 Comments

Thanks Stephen,
I know! but it worked fine up till now!
So if you think that my readcell command won't work the way i want anymore, the question now is : do u know a way in which i can read a col from excel starting from B3 and stops when there is nomore data in the col. ..
example: col. having numerical values starting from B3 and ends at B16 in the first run, then a col. have data staring from B3 and ends at B200!
P.S: I can't use a for loop to filter empty values because this col. is linked to others and if i do this, itwould complicate the processing afterwards.
Thank you again for your help.
sorry, data in the col ar not numercal data, but data like F1, F2,, F3 etc..
It is not required to specify the end row or end cell of a range, as the readcell 'Range' option supports:
  • Starting Cell
  • Starting Row Number
  • Empty -> UsedRange
All of these will automatically read the file data from the specified cell or row until the last used row. Clearly a function which requires the user to predict/guess/know exactly where the data ends would be quite inconvenient to use, which is why readcell supports those syntaxes which figure it out for you.
But these will also read blocks of data, not just one column, so they might not give just the data you want.
Another option is to specify the entire column, e.g.:
'Range','B3:B1048576'
The documentation states that readcell "..automatically detects the column extent by reading from the first nonempty column to the end of the data" (emphasis added), which might give exactly the data you want. I do not have a version of MATLAB with this function, so you will have to test that yourself :)
yes!! it works again, thanks !
I have used
x1 = readcell("model","sheet","Global FCM","Range",'B3');
which gives me a 14x86 array
then i used x1= x1(:,1) it just gave me the result :)
Thanks a lot!
Have a nice day!
Lara
is there a way to use cell numbers to specify a a range?
Like [2 3] instead of 'B3'.
I tried x1 = readcell("model","sheet","Global FCM","Range",[2 3]); and got the error "Unable to determine range".
Is this a syntax problem or simply cannot be done?
"is there a way to use cell numbers to specify a a range?"
Yes, according to the documentation the current READCELL permits a 2-element (start cell) and 4-element (start and end cells) numeric vectors for the RANGE option:
"Is this a syntax problem or simply cannot be done?"
It can be done. Lets try it here (sample file is attached, with UsedRange from A1 to C3):
C = readcell('test.xlsx','Range',[2,2])
C = 2×2 cell array
{[22]} {[23]} {[32]} {[33]}
That gives exactly the expected output without error.
The error message you showed sounds like there is some ambiguity with your worksheet. But if you do not upload a sample workbook by clicking the paperclip button then I cannot debug this for you.

Sign in to comment.

More Answers (0)

Tags

Asked:

on 23 Mar 2020

Edited:

on 8 Jun 2022

Community Treasure Hunt

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

Start Hunting!