Hi Maximilian,
Simulink software can register a listener only while a simulation is running. Invoking the function "myfunction" when no simulation is running results in an error message. To ensure that a listener catches all relevant events triggered by a model's simulation, you should register the listener in the model's StartFcn callback function.
add_exec_event_listener returns a handle to the listener that it registered. So you may try using the command with the following format:
h = add_exec_event_listener(blk,event,listener)
Also, you may refer to the documentation link below:
Thanks