MATLAB Answers


Undefined function 'fetch' for input arguments of type 'struct'. when reading and writing to an Access Database

Asked by Lachlan
on 5 Feb 2013
Latest activity Commented on by Fox Peterson
on 2 Mar 2014

Hi all,

This is my first question - please accept my apologies if I format incorrectly.

I call the below code in an infinite loop which runs smoothly for the first ~200 iterations then catches the "Undefined function 'fetch' for input arguments of type 'struct'." error. It is worth noting that in between each iteration I open and close several other tables within the same ODBC connection before reconnecting to this one.

What has me confused is that it runs as it should for the first few hundred iterations. My hunch is that one connection may not close properly but Im not certain.

setdbprefs('DataReturnFormat', 'cellarray');
setdbprefs('NullNumberRead', 'NaN');
setdbprefs('NullStringRead', 'null');
conn = database('ACCESS DB', '', '');
curs = exec(conn, ['SELECT 	TABLE1.ID_DB'...
    ' FROM 	TABLE1 ']);
curs = fetch(curs);

Thanks in advance for any help!

  1 Comment

I thought I would add my silly user experience here, also, because I wasted too much time on the same error for a stupid reason:

Make sure if you used the query builder in the Database toolbox to build your query that you go back into the script and manually type in your password, since it defaults to blank. I was wondering why I couldn't get a connection... I put the password in and of course it was smooth sailing from there.

1 Answer

Answer by the cyclist
on 5 Feb 2013
 Accepted answer

My guess is that you are losing the connection to the database, but I have no idea why. I suggest you do the following. Type

>> dbstop if error

before you try to run that loop. Then execute your code. When the error occurs, the execution will halt, but you will be taken to the editor, at the line where the error occurred. You can see what the variables look like at the time of the error.

My version of your code ran for 500+ iterations, but when I deliberately broke the connection (by disconnecting my VPN), the code halted with the same error you got. The proximate cause of the error is that "curs", instead of being a full cursor object, is instead a structure that is just

curs.Message = 'Invalid connection.'

  1 Comment

on 5 Feb 2013

Thanks cyclist for your response.

I have tried your suggestion which yields a different error "sun.jdbc.odbc.JdbcOdbcBatchUpdateException: General error". However in the process of doing this I think I have discovered the root cause. As the loop was running I opened up the database to confirm that the data was being written, in doing so I selected a cell inadvertently. The next loop then generates the error. This still has me a little confused as I had closed Access before re-executing the code only I got the error immediately. I think this confirms you correct that I was still connected to the DB form the previous execution and thus couldn't connect. It looks fine if I don't open it up mess around.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!