Documentation:Reference:Matlab - MdsWiki
Navigation
Personal tools

From MdsWiki

Jump to: navigation, search

Contents

Reading and Writing

If there is an MDSplus server on your Local Area Network at address 123.456.7.89 and a tree on the server named mytree then you connect and read data like this:

 mdsconnect('123.456.7.89')
 mdsopen('mytree',42)
 somenumbers = mdsvalue('\data_node')
 mdsclose
 mdsdisconnect

In the above example we opened shot 42 of mytree and read data from the node with tag name data_node into a Matlab workspace variable named somenumbers. Most of the commands above will return some kind of answer when executed. Of importance is the fact that the mdsopen command will return the pulse number if successful and nothing if unsuccessful.

To write data simply use the mdsput command:

 mdsput('\data_node',somenumbers)

In the above example somenumbers can be any Matlab variable, including multi-dimensional arrays.

Decoding Error Messages

MDSplus can sometimes answer a request with a cryptic error message number. For example, if you try to store text in a numeric usage node you get the return code 265388426. Fortunately we can ask the MDSplus server to explain things with the mdsgetmsg command. Try this one:

 mdsgetmsg(265388426)

More Tools

Matlab is useful mainly as an analysis tool. In this role it is important to be able to read data from MDSplus trees and perhaps be able to write results back in. But if you really want to you can issue Tree Command Language (TCL) commands to the MDSplus server through Matlab. You do this by using the mdsvalue m-file.

 mdsvalue('tcl($)','set current mytree 42')

In this example I set the current pulse number for mytree to 42. In general you can issue any TCL command in this way, as long as you have permission on the server to execute the command.

You can also query the MDSplus server to find out what the current shot number is. The TDI command is somewhat hard to find in the documentation, but it goes like this:

 mdsvalue('current_shot("mytree")')

Setup

You can access MDSplus from Matlab via java or python. Once MAtlab can find the .m files in $MDSPLUS_DIR/matlab you can run

mdstest(0) % to test the java bridge
mdstest(1) % to test the python bridge


Setting the Matlab Search Path

Once you have installed the MDSplus software on your desktop computer you can read and write data to and from MDSplus trees using Matlab. The m-file functions for MDSplus operations will be in your MDSplus directory. You should add this folder to your Matlab search path using the Set Path... option from the File menu or via:

%% add $MDSPLUS_DIR/matlab to path
addpath(fullfile(getenv('MDSPLUS_DIR'), 'matlab'))
savepath()

Additional configuration may be needed to use MDSplus from Matlab depending on the preferred API (java or python). Both APIs should work interchangeably but depending on your configuration one may be more performant than the other.

tic;mdstest(0);toc; % time java
tic;mdstest(1);toc; % time python

You may want to time it multiple times to get a settled (with cached libs) value. On Windows machines the java API is typically faster.

Setting up access via java mdsobjects.jar

By default the Matlab API is bridging thru mdsobjects.jar.

Modern versions of Matlab support java libraries to be imported on-the-fly and you only need to set the Matlab search path.

If Matlab complains about unsatisfied links you may need to execute the following lines and restart MATLAB:

%system(sprintf('echo %s> %s',[regexprep(getenv('MDSPLUS_DIR'),'\\','/'), '/lib'],fullfile(prefdir,'librarypath.txt')));%old versions
system(sprintf('echo %s> %s',fullfile(getenv('MDSPLUS_DIR'),'lib'),fullfile(prefdir,'javalibrarypath.txt')));%new versions
system(sprintf('echo %s> %s',fullfile(getenv('MDSPLUS_DIR'),'java','classes','mdsobjects.jar'),fullfile(prefdir,'javaclasspath.txt')));

Setting up access via python API

To activate the python API make sure the python MDSplus package can be found in your PYTHONPATH.

Once at the beginning of you session you can activate if using

mdsUsePython(true)

or add the command to you startup.m file.

Troubleshooting

See: MATLAB Specific Issues