Parameters in Matlab stream function | LabJack
 

Parameters in Matlab stream function

4 posts / 0 new
Last post
longluu
longluu's picture
Parameters in Matlab stream function

In the example code u6_simplestream.m, lines 103-108 reads like this:

    % Get the enums for LJ_ioGET_STREAM_DATA and LJ_chALL_CHANNELS which we use
    % in the read stream data loop.
    typeIO = ljasm.AssemblyHandle.GetType('LabJack.LabJackUD.LJUD+IO');
    LJ_ioGET_STREAM_DATA = typeIO.GetEnumValues.Get(22);  % Use enum index for GET_STREAM_DATA 
    typeCHANNEL = ljasm.AssemblyHandle.GetType('LabJack.LabJackUD.LJUD+CHANNEL');
    LJ_chALL_CHANNELS = typeCHANNEL.GetEnumValues.Get(99);  % Use the enum index for ALL_CHANNELS

I wonder what the number 22 in typeIO.GetEnumValues.Get(22) and the number 99 in typeCHANNEL.GetEnumValues.Get(99); indicate. Thanks for any help on clarifying that.

LabJack Support
labjack support's picture
The eGetPtr function has

The eGetPtr function has LabJack.LabJackUD.LJUD+IO and LabJack.LabJackUD.LJUD+CHANNEL enumerator parameters.

typeIO.GetEnumValues gets the enumerator list for LabJack.LabJackUD.LJUD+IO, and typeCHANNEL.GetEnumValues gets the enumerator list for LabJack.LabJackUD.LJUD+CHANNEL.

typeIO.GetEnumValues has 54 elements, where element 22 is the GET_STREAM_DATA enumerator. typeCHANNEL.GetEnumValues has 100 elements, where the ALL_CHANNELS enumerator is element 99.

typeIO.GetEnumValues.Get and typeCHANNEL.GetEnumValues.Get gets an enumerator based on the index in the list. Note that these are .NET methods where indexing starts at 0.

If you know the name of the enumerator in the enumerator list, you can find the index with something like:

typeIO = ljasm.AssemblyHandle.GetType('LabJack.LabJackUD.LJUD+IO');

% Get the string list of enumerator names and convert them to a MATLAB array of strings.
strs = string(typeIO.GetEnumNames());

% Search for the 'GET_STREAM_DATA' enumerator name, getting its MATLAB index.
% Subtract 1 from the returned value to account for .NET indexing.
index = find(strs == 'GET_STREAM_DATA')-1

Note that "strs" is a MATLAB array where indexing starts at 1. Since indexing start at 0 for typeIO.GetEnumValues.Get and .NET arrays, 1 is subtracted from the index returned by find. A .NET array of strings is not compatible with the find function, and is the reason for the MATLAB string conversion.

longluu
longluu's picture
Thanks for the quick and

Thanks for the quick and thorough answer. I really appreciate that.

So if I just need to get input from one analog channel, should I use another parameter from typeCHANNEL.GetEnumValues  because it seems like 99 is for all channels?

Also I tried the code you posted to get the list of enumerator names:

typeIO = ljasm.AssemblyHandle.GetType('LabJack.LabJackUD.LJUD+IO');

% Get the string list of enumerator names and convert them to a MATLAB array of strings.
strs = string(typeIO.GetEnumNames());

And Matlab returns this error

Warning: string is obsolete and will be discontinued.
         Use char instead. 
Error using string
Conversion to char from System.String[] is not possible.

I use Matlab 2014a. Do you know what is going on?

Thanks.

Long

 

LabJack Support
labjack support's picture
LJ_chALL_CHANNELS retrieves

LJ_chALL_CHANNELS retrieves all data interlaced into a 1D array. So if streaming 2 channels and using LJ_chALL_CHANNELS, the stream data order will be:

[channel0-scan0, channel1-scan0, channel0-scan1, channel1-scan1, channel0-scan2, channel1-scan2, etc...]

If you only want the samples of one channel returned, an enumerator is not needed for the third parameter of eGetPtr and you pass the channel number as an integer value.

I tested that code in 2018b, and it didn't have issues with string or the warning. Judging from your error, it seems in older MATLAB versions, such as 2014a, string cannot convert an array of .NET System.String. I don't have that version to test on hand, but something like this may work instead:

typeIO = ljasm.AssemblyHandle.GetType('LabJack.LabJackUD.LJUD+IO');

% Get the string list of enumerator names and convert them to a MATLAB array of strings.
names = typeIO.GetEnumNames();
strs = [""];
for i = 1:54
    strs(i) = string(names(i));  % or use char instead of string
end

% Search for the 'GET_STREAM_DATA' enumerator name, getting its MATLAB index.
% Subtract 1 from the returned value to account for .NET indexing.
index = find(strs == 'GET_STREAM_DATA')-1

Since you are using MATLAB 2014a, and in general for versions 2009a to 2017b, you can access an enumerator directly like so:

LabJack.LabJackUD.IO.GET_STREAM_DATA

Our older examples dated 06/24/2014 use this method. Changes in MATLAB 2018a+ prevent usage like that and the current examples use the GetEnumValues.Get method to access an enumerator. Details about this are in the MATLAB download's README.txt.