datajoint-matlab icon indicating copy to clipboard operation
datajoint-matlab copied to clipboard

discovering existing tables across python/matlab

Open mikewehr opened this issue 3 years ago • 5 comments

Hi, we're just getting started implementing datajoint in our lab, using a local docker server. Jonny, a python user, started by adding subject schemas with basic biographical info. I, a matlab user, can connect to the database but do not know in advance what schemas have been created in python. How do I discover which schemas/tables already exist in the database and connect matlab schemas to them? Is this documented somewhere? This docs page https://docs.datajoint.org/matlab/existing/5-Loading-Classes.html didn't seem to help me, unless I'm missing something. Thank you!

Reproducibility

  • Ubuntu 21.10
  • MATLAB Version 2021b
  • MySQL Version - docker image 8.0
  • MySQL Deployment Strategy = local-docker
  • DataJoint Version 3.5.0

mikewehr avatar Apr 26 '22 21:04 mikewehr

The datajoint python function list_schemas has the SQL syntax for querying the available schemas. Within MATLAB, that SQL syntax could be embedded in a query().

There's probably a more native way to do the same, but this is the first thing that comes to mind

CBroz1 avatar Apr 26 '22 21:04 CBroz1

To connect to an existing schema from matlab, you need to know its name. You can list the schema names using SQL commands:

conn = dj.conn()
conn.query('SHOW SCHEMAS')

Once you know the schema name, you you can connect to it using the dj.createSchema function. This will match the corresponding database schema to a MATLAB package (a +package folder) with the getSchema function inside it.

Then you can plot the diagram of that schema using the erd command

erd <package>

You can access the schema using the schema object, which you can instantiate by calling the getSchema function in the package

schema = package.getSchema()

With this schema object, you can use its dynamic property .v to access any table as if it was a table class.

schema.v.Scan

Alternatively, you can use the dj.new function to create a full-blow class for the given table. If the table already exists, it will create the matlab classdef file with the full definition reverse-engineered from the database definition.

dimitri-yatsenko avatar Apr 26 '22 22:04 dimitri-yatsenko

Thank you, this was super helpful. Did I miss this in the documentation, or is this a topic that could be more completely documented? Do you encourage users to contribute to docs with PRs?

mikewehr avatar Apr 27 '22 01:04 mikewehr

Our documentation is lagging behind a bit, especially with matlab. I would be very happy to see a user-contributed PR to our docs repository.

CBroz1 avatar Apr 27 '22 13:04 CBroz1

@mikewehr Your contributions in both functionality and documentation are welcome! We will be refactoring the documentation to make contributions easier.

dimitri-yatsenko avatar Apr 27 '22 14:04 dimitri-yatsenko