3
votes

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
can you add a minimal reproducible example of how the block looks like so we can reproduce it and try ti? - Ander Biguri
Maybe change to an S-function, instead of Matlab function block? - rinkert
It is not my own models, I cannot change any major things. - Smartskaft2
For MATLAB function block, code is saved in the model. So you cannot open or edit it without opening the model. Once the model is open you can however get or set the code programmatically as a string. For e.g. stackoverflow.com/questions/42263697/…. Alternatively, you can change your code in MATLAB Function block to call a .m file which you can edit easily. - Navan
I am writing functions to help me find and highlight blocks in simulink model. I am lazy and would like to write a function that together with these, opens the code in the matlab editor. I could then load the model, find the code blocks and open the code in the editor. All without opening a Simulink window. - Smartskaft2

2 Answers

2
votes

You can open the code in the Editor using,

view(blockHandles(iHandle))
0
votes

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.