0
votes

I am a little confused about the described behavior of Matlab. What I am doing is reading data from a CAN Bus with the "Vehicle Communication Toolbox" and live plotting the data in my GUI.

For reading the CAN data, the Toolbox calls a Callback function. It reads and filters the CAN Data and stores Timestamps and Data in my own data structure.

For updating the GUI I have defined a timer event which is called every 0.1 seconds. It reads the data from my data structure and assigns it to the 'XData' and 'YData' properties of some plots.

After a random amount of time I get warnings that XData and YData do not have the same length.

Debugging showed me that the callback function of my timer object interrupted the callback function that reads the CAN Data just between the assignment of the timestamp (XData) and the Signals(YData) to my data structure.

There is no call to a drawnow function or sth. Or else that allows the event queue to process the next event.

Here is a snippet of my callback function for new CAN Messages

% save timestamps
% this line my be interrupted:
this.canData.(msgInfo.Name).Timestamps = ...
   [this.canData.(msgInfo.Name).Timestamps ...
   [messages.Timestamp]];

%save all Signals in a Message, they all have the same timestamp
% this line my be interrupted:
signals = [messages.Signals];   
for s = 1:length(msgInfo.Signals)
    this.canData.(msgInfo.Name).Signals.(msgInfo.Signals{s}).Data = ...
    [this.canData.(msgInfo.Name).Signals.(msgInfo.Signals{s}).Data ...
    signals.(msgInfo.Signals{s}) ];
end

In debugging the execution stops in one of the two lines and continues in timercb.m where my timer functions is called, which then tries to update the plot and generates a warning.

Is this behavior somewhere documented? The documentation sais, only calls to drawnow, waitfor etc. allows the interruption of a function.

I am using Matlab 2015a


Update

The Matlab profiler showed me where the timer function "timercb" gets called. I am really wondering why Matlab stops all these functions somewhere. They all have no call to drawnow, wait, or sth. else. Profile

I know, Matlab is single threaded, but how does this fit together?

1

1 Answers

0
votes

I assume you are using the MessageReceivedFcn for a CAN channel as described here. Can you instead use the receive() function to explicitly poll the CAN channel during your timer callback? That way there would be no chance of interruption and data changing at the wrong time.

The receive() function returns an indication of number of messages received, so you would just need to check if it's non-zero.