Matlab OSX read AIN response | LabJack
 

Matlab OSX read AIN response

3 posts / 0 new
Last post
cbrandon
cbrandon's picture
Matlab OSX read AIN response

Using another user's Matlab code found on the legacy forums (Github: labJack.m) to call exodriver functions, I have run into an issue when trying to read analog input. I am successfully writing and reading the correct number of bytes when using the ConfigIO and Feedback functions. I have confirmed my input using C instead of Matlab and have no issues (I can access the return bytes with 0 errors), but when running in Matlab, it returns the correct number of bytes read, but it does not update the array that is passed in. I am initializing an array of zeros to pass into LJUSB_ReadTO, it reads the appropriate number of bytes, but my array is untouched. I am using the rawRead function within labjack.m:

        function in = rawRead(obj,bytein,count)

            if ~exist('bytein','var')

                bytein = zeros(10,1);

            end

            if ~exist('count','var') || count > length(bytein)

                count = length(bytein);

            end

            in =  calllib('liblabjackusb', 'LJUSB_ReadTO', obj.handle, bytein, count, obj.timeOut);

            if in == 0; obj.salutation('rawRead','ERROR READING!',true); end

        end

For some reason, "transferred" from the exodriver is returning the number of bytes read, but not updating my bytein array. How can I save the data read from AIN in Matlab?

LabJack Support
labjack support's picture
The rawRead function doesn't

The rawRead function doesn't return the read byte array or update bytein, and needs to be modified to do so. This call in rawRead:

in =  calllib('liblabjackusb', 'LJUSB_ReadTO', obj.handle, bytein, count, obj.timeOut);

Should be something like this instead:

%"bytein" replaced by "bytes", "in" replaced by "ret_count"
[ret_count, ret_handle, ret_bytes] = calllib('liblabjackusb', 'LJUSB_ReadTO', obj.handle, bytes, count, obj.timeOut);

And return ret_bytes.

I helped another user regarding this here:

https://labjack.com/forums/u3/seeking-advice-how-use-u3-measure-pedal-st...

In particular, post 17 provides some code for an alternative rawRead function that does return the updated response byte array.

cbrandon
cbrandon's picture
Great! This is very helpful.

Great! This is very helpful. I wish I had found that thread sooner and saved myself the confusion! Thank you.