Q:

Is it possible to send CAN messages out of a callback function?

I have following problem: I would like to send CAN messages out of a callback function. This callback function is called by an timerobject. Everytime when the callback function is executed it should send the CAN message.
I coded:
canch = canChannel('PEAK-System', 'PCAN_USBBUS1'); 
configBusSpeed(canch, 1000000)  
start(canch) 

canch.Database = canDatabase('\\psf\Home\Desktop\02 CAN DBC\CAN4.dbc'); 

message = canMessage (canch.Database,'IPSS_RX_1_1ms'); 

transmitPeriodic(canch,message, 'On', 0.01); 

T= timer('ExecutionMode','fixedrate','Period',0.2,...
         'TimerFcn',@TEST_timer_callback_fcn,'StopFcn','disp(''Timer has stopped'')');

start(T) 

Callback function:
function TEST_timer_callback_fcn(src,event)

message.Signals.In_ti= 39;

The CAN Channel starts - but the message is not send.
Can somebody help?

A:

It's likely that your timer function simply doesn't have access to the message object (i.e. it exists in a different scope). One option is to nest your timer function within a function where you initialize message. Alternatively, you can pass message to your timer function. You could do this by changing the timer creation line to this:
T = timer('ExecutionMode','fixedrate','Period',0.2,...
          'TimerFcn',@(~,~) TEST_timer_callback_fcn(message),...
          'StopFcn','disp(''Timer has stopped'')');

And the callback function to this:
function TEST_timer_callback_fcn(message)

  message.Signals.In_ti= 39;

