Programmatically open a Simulink MATLAB function block's code

1.7k views Asked by At

Can I open a local Simulink MATLAB function block's code in the MATLAB editor via some command?

For example, let us say I have a Simulink model named mainModel.slx.

In it, there is a MATLAB function block named localFunction. This is not defined in a .m-file.

I would be able to edit the function which path is mainModel/localFunction, without having to open the simulink window and double click on the function block. Is this possible?

I have of course already tried open mainModel/localFunction and edit mainModel/localFunction. I have access to the handle for its StateFlow.EMChart object.


EDIT: Minimal, (hopefully) Complete and Verifiable Example

My minimal Simulink model is shown in the picture below. Code is present below it. For readability, I have not addressed bugs or glitches. It is not for general usage.

enter image description here

The function code for the MATLAB function block localFunction is

function y = fcn(u)
   y = 'findThis'; % I want to end up here, in the MATLAB editor!
end

I am using the following code to load the model, search through all MATLAB function blocks and find the ones containing the string 'findThis'. The MATLAB function block named 'localFunction' should then be found. Again, ignore the bugs. The code is saved in a script called tmpScript.m.

% User set
model       = 'mainModel';
expression  = 'findThis';
blockType   = 'Stateflow.EMChart'; % MATLAB function block, right?

% Load model
load_system(model)

% Find all MATLAB function block handles
blockHandles = find(slroot, '-isa', blockType);

% Find first block containing the defined expression
for iHandle = 1:numel(blockHandles)
   tmpFind = strfind(blockHandles(iHandle).Script, expression);
   if ~isempty(tmpFind)
      break
   end
end
foundBlockPath = blockHandles(iHandle ).Path; % Function block path
foundCharIdx   = tmpFind;                     % Character index

% Print results in command window
fprintf('Function path: %s\n', foundBlockPath)
fprintf('Character index: %d\n', foundCharIdx)

In this example, the path should be mainModel/localFunction and the character index 29 (Note the three leading white spaces on the function's second line, and that the line break '\n' is worth one characters). The command window shows

>> tmpScript
Function path: mainModel/localFunction
Character index: 29
>>

I can thus load models and search through their MATLAB function blocks for specific strings. When I have found this function, I would like to be able to open it in the matlab editor. What is called when I double click on the block in the Simulink window?

These do NOT work

open(foundBlockPath)
edit(foundBlockPath)
blockHandles(iHandle).openEditor

I cannot change the Simulink model itself. I do not want to change the function script. I just want to be able to open it in the MATLAB editor.

2

There are 2 answers

1
Phil Goddard On BEST ANSWER

You can open the code in the Editor using,

view(blockHandles(iHandle))
1
rinkert On

You could change the Matlab function block to an Interpreted Matlab function block.

This does have the limitation that it only can have one input and one output (which can be vectors), so depending on your problem, you might have to mux/demux some data.

Alternatively you can change to an S-function, which gives more flexibility, but might be a bit more complex to setup.