« Close

Datasheets and User Guides

App Notes

Software & Driver

 

MATLAB for LJM - Windows

LJM MATLAB Examples Windows (Software Platform)

MATLAB .NET example scripts for the LJM library.

Release

LJM MATLAB Examples (Referencable)

Prerequisites

  • LabJack T7 or T4
  • LJM Library and .NET assembly - Install the LJM Library
  • MATLAB 7.8 (R2009a) or newer
  • Operating system - Windows

Instructions

  1. Download/extract the .zip file attached at the top of this page.
  2. In MATLAB open, edit and run the script examples in extracted MATLAB_LJM folder. 

Note: Add example scripts to the MATLAB path before running, and all examples require the functions in showDeviceInfo.m and showErrorMessage.m.

Refer to the download's README.txt file for more details.

Code Snippet

%Make the LJM .NET assembly visible in MATLAB
ljmAsm = NET.addAssembly('LabJack.LJM');

handle = 0;

try
    %Open first found LabJack
    [ljmError, handle] = LabJack.LJM.OpenS('ANY', 'ANY', 'ANY', handle);
    
    %Call eReadNames to read the serial number from the LabJack.
    name = 'SERIAL_NUMBER';
    [ljmError, value] = LabJack.LJM.eReadName(handle, name, 0);
    
    disp('eReadName result:')
    disp(['  ' name ' = ' num2str(value)])
catch e
    showErrorMessage(e)
end

17 comments

Hi,

We have a problem that we hope that you can help us with. 

We have bought your LabJack T7, and installed it, and we are able to control it from our computer through Ethernet connection. We use a Windows XP with .NET version 4.0 and matlab 2013b. Our problem is that we can't seem to make the 'Wait' routine work. When using your example code 'eWriteNames.m' it reports the error:

''LabJack.LJMExceptions: LJME_OLD_Firmware,   Error in using: (code line containing LabJack.LJM.eWriteNames()),   Messeage: LJME_OLD_FIRMWARE,   Source: LabJack.LJM''

The only thing edited in the code is the ethernet address of the LabJack.

It works without ' WAIT_US_BLOCKING', which we have tested by out commenting the part in the code containing aNames(5)=' WAIT_US_BLOCKING' and  aValues(5)=100, redefine NumFrames from 7 to 6, and removing one entry from aNames and aValues, such that num 6 and 7 becomes 5 and 6. T7 sends out its pulses as it should by doing this. 

We experience the same if we use eWriteAddresses, using what we understand to be the 'Wait' routines address 61590. It results in similar error. Again we have no error if we do not try to use a 'Wait'

So can you tell us what it is that goes wrong, and more important how to make the Wait routine work?

Regards Erik,

First thing I notice is the "old firmware" message you are getting, most likely because WAIT is a recently added feature.  Make sure you have the latest software (LJM 1.0300) and update to the latest T7 beta firmware.

On my system, I keep getting errors because functions like 'showDeviceInfo.m' and 'showErrorMessage.m' are in the parent folder ('\Examples'). This can be solved by adding this folder to the matlab path, or by inserting addpath('..') at the start of each individual example m-file.

That is something we can look into adding. In the README.txt we mention to "Add to Path" the MATLAB_LJM folder and scripts which resolves those kind of errors.

When looking at the example SingleAINWithConfig.m (for example...) I notice the use of NET.createArray methods, such as:

aNames = NET.createArray('System.String', numFrames);

aNames(1) = 'AIN0_NEGATIVE_CH';

aNames(2) = 'AIN0_RANGE';

aNames(3) = 'AIN0_RESOLUTION_INDEX';

However, it appears to me that e.g. aNames = {'AIN0_NEGATIVE_CH', 'AIN0_RANGE', 'AIN0_RESOLUTION_INDEX'} also works.

Could you tell me when the use of NET.createArray() is absolutely required?

It's needed when passing an array to a .NET method such as LabJack.LJM.eWriteNames, or at least it is required in older versions of MATLAB such as R2009b. Passing an array of "{'AIN0_NEGATIVE_CH', 'AIN0_RANGE', 'AIN0_RESOLUTION_INDEX'}" to eWriteNames in R2009b causes this kind of error:

No method 'LabJack.LJM.eWriteNames' with matching signature found.

We currently don't have a newer version of MATLAB to test and confirm with, but NET.createArray may not be necessary in newer MATLAB versions. You can try using normal arrays (not built with NET.createArray) with other LJM methods and see if they work.

After doing some tests, to update my first response a call like this works in R2009b where NET.createArray is not required for an array of doubles but NET.createArray is needed for the array of strings. As mentioned earlier, NET.createArray may not be necessary for array of strings in newer MATLAB versions:

numFrames = 3;
aNames = NET.createArray('System.String', numFrames);
aNames(1) = 'AIN0_NEGATIVE_CH';
aNames(2) = 'AIN0_RANGE';
aNames(3) = 'AIN0_RESOLUTION_INDEX';
aValues = double([199, 10, 0]); %Need to specify this is an array of double type
LabJack.LJM.eWriteNames(handle, numFrames, aNames, aValues, 0);

Thanks for the information. I could be wrong, but after playing around with it some more, it seems that (for R2014a) the NET.createArray() method is only required whenever the variable is passed into a function by reference, in order that the function can change its value. That is, whenever the variable is used as an output variable. This is otherwise not possible in Matlab (as far as I know), as the Matlab syntax uses explicit output arguments instead of pointers. For example, it seems that aAddresses and aTypes, which are output variables in LJM_NamesToAddresses(), need to be created using NET.createArray(), but aNames does not. Could this be correct?

If that is what you observed, then yes it sounds like output parameters that are arrays need NET.createArray() and input only array parameters do not (including array of strings), at least in MATLAB R2014a.

In older MATLAB versions, such as R2009b, create NET.createArray() is needed when passing an array of strings parameter in general, both input and output, and when the parameter is an output array regardless of data type (an error isn't given but the array's values do not get updated after the call).

 

I tried to sample data from labjack by using the example code, 'DualAINLoop.m'.

But, it looks the sampling doesn't work properly when a delay time less than 0.1 sec is used.

When I use the delay time of 0.01 as below, it takes ~16 seconds for 1000 loops. 

 

 delay = 0.01; %delay between readings (in sec)

 for i=1:1000,

        pause(delay);

if mod(i,1000)==0

        disp(i)

end

end

 

Can you recommend different method to sample a data with a higher sampling rate by using Matlab?

 

 

What sampling rate are you looking for and what are you sampling?

You can do the readings without the pause or a lower pause time (if possible in MATLAB). No pauses will give you the fastest command/response rates in MATLAB. Note that T7 communications and processing also have a delay. The "Testing\CRSpeedTest.m" example can be used for command/response speed tests.

For the fastest speeds (up to 100K samples per second), use stream mode. It is hardware timed and you are reading buffered data from the T7. Take a look at the "Stream\SimpleStream.m" example.

General command/response and stream mode data rates are documented here:

http://labjack.com/support/datasheets/t7/appendix-a-1

T7 documentation can be found here:

http://labjack.com/support/datasheets/t7

Can you please guide me how to use an example provided from you,also in Simulink?

Please try our examples listed above and let us know what issues you run into.  The examples are all ".m" files and can be run like any other matlab example.  Try opening the Examples/AIN/SingleAIN.m file and running it, it will open any T7/T7-Pro connected to your computer and read AIN0.

As for Simulink, we don't provide any specific Simulink examples however Simulink can run .m files have a look at this Matlab support link, running m-file from Simulink.

Thank you for your quick response.  Actually I wont to elaborate my dissertation and the final scope is to be able to control(adjust) the rpm of a dc motor.

For example I tried this: I opened Matlab and was able to see the "result" in the Command Viewer of the SingleDIORead.m 

After that I opened Simulink and I added this 3 bloks : a step,a matlab function and a display. I copy-paste the code from the m.file inside the matlab function and I was hoping to see the "result" in the display block. 

Any suggestions or advice? I want to add also a PID but first I want to be able to read and write a DIO from simulink...

Thank you in advance!

I haven't used Simulink, but it looks like you make a MATLAB function with a return/output and send that to the display. So the function declaration could look something like:

function state = SingleDIORead
%SingleDIORead.m code

You then send your function's "state" output to the display block. Here's some helpful links I found relating to this:

http://www.mathworks.com/help/simulink/ug/creating-an-example-model-that...

http://www.goddardconsulting.ca/simulink-using-embedded-matlab.html

I copy -paste the code lines from SingleAIN.m in a matlab function and I tried to adjust a little bit in order to have the right input/output for the function but I get this errors

1.Function 'NET.addAssembly' is not supported for code generation. Consider adding coder.extrinsic('NET.addAssembly') at the top of the function to bypass code generation.

2.Undefined function or variable 'ljmAsm'. The first assignment to a local variable determines its class.

3.Undefined function or variable 'System.Activator'

4. Try and catch are not supported for code generation.    and many others

Except 2,3 that I think I can fix them,do you think that in Simulink we have a .Net limitation?

I'm not 100% sure about the Simulink function block and .NET, but it seems like there might be a limitation with code generation and building a standalone application. For a definite answer you will want to ask MathWorks.

It does look like you can use coder.extrinsic to run the .NET related code in MATLAB/Simulink. That should help with problem 2 as the NET.addAssembly sets/returns ljmAsm. As for System.Activator.CreateInstance, you can try using code.extrinsic to make it available or comment out that line. If using only the LJM.OpenS and eRead/eWrite Name/s LJM .NET functions you don't need constants.

You could however make a MATLAB function/script to run the .NET/LJM related code, and have the Simulink function block call that function with coder.entrinsic so you don't have to call that throughout your code. This was mentioned in this answer:

http://www.mathworks.com/matlabcentral/answers/169990-simulink-how-to-ca...

So a MATLAB function like this:

function state = SingleDIOReadWrapper
%.NET/LJM code (don't use coder.entrinsic)

And then the Simulink function block would call it:

function state = SingleDIORead
coder.entrinsic(SingleDIOReadWrapper)
state = SingleDIOReadWrapper()