Programmatically open a Simulink MATLAB function block's code










3















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: %sn', foundBlockPath)
fprintf('Character index: %dn', 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.










share|improve this question
























  • can you add a Minimal, Complete, and Verifiable example of how the block looks like so we can reproduce it and try ti?

    – Ander Biguri
    Nov 13 '18 at 11:17











  • Maybe change to an S-function, instead of Matlab function block?

    – rinkert
    Nov 13 '18 at 12:23











  • It is not my own models, I cannot change any major things.

    – Smartskaft2
    Nov 13 '18 at 12:59











  • 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
    Nov 13 '18 at 14:47












  • 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
    Nov 13 '18 at 15:26















3















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: %sn', foundBlockPath)
fprintf('Character index: %dn', 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.










share|improve this question
























  • can you add a Minimal, Complete, and Verifiable example of how the block looks like so we can reproduce it and try ti?

    – Ander Biguri
    Nov 13 '18 at 11:17











  • Maybe change to an S-function, instead of Matlab function block?

    – rinkert
    Nov 13 '18 at 12:23











  • It is not my own models, I cannot change any major things.

    – Smartskaft2
    Nov 13 '18 at 12:59











  • 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
    Nov 13 '18 at 14:47












  • 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
    Nov 13 '18 at 15:26













3












3








3








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: %sn', foundBlockPath)
fprintf('Character index: %dn', 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.










share|improve this question
















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: %sn', foundBlockPath)
fprintf('Character index: %dn', 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.







matlab simulink stateflow






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 16:28









Phil Goddard

8,9271823




8,9271823










asked Nov 13 '18 at 10:45









Smartskaft2Smartskaft2

639




639












  • can you add a Minimal, Complete, and Verifiable example of how the block looks like so we can reproduce it and try ti?

    – Ander Biguri
    Nov 13 '18 at 11:17











  • Maybe change to an S-function, instead of Matlab function block?

    – rinkert
    Nov 13 '18 at 12:23











  • It is not my own models, I cannot change any major things.

    – Smartskaft2
    Nov 13 '18 at 12:59











  • 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
    Nov 13 '18 at 14:47












  • 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
    Nov 13 '18 at 15:26

















  • can you add a Minimal, Complete, and Verifiable example of how the block looks like so we can reproduce it and try ti?

    – Ander Biguri
    Nov 13 '18 at 11:17











  • Maybe change to an S-function, instead of Matlab function block?

    – rinkert
    Nov 13 '18 at 12:23











  • It is not my own models, I cannot change any major things.

    – Smartskaft2
    Nov 13 '18 at 12:59











  • 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
    Nov 13 '18 at 14:47












  • 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
    Nov 13 '18 at 15:26
















can you add a Minimal, Complete, and Verifiable example of how the block looks like so we can reproduce it and try ti?

– Ander Biguri
Nov 13 '18 at 11:17





can you add a Minimal, Complete, and Verifiable example of how the block looks like so we can reproduce it and try ti?

– Ander Biguri
Nov 13 '18 at 11:17













Maybe change to an S-function, instead of Matlab function block?

– rinkert
Nov 13 '18 at 12:23





Maybe change to an S-function, instead of Matlab function block?

– rinkert
Nov 13 '18 at 12:23













It is not my own models, I cannot change any major things.

– Smartskaft2
Nov 13 '18 at 12:59





It is not my own models, I cannot change any major things.

– Smartskaft2
Nov 13 '18 at 12:59













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
Nov 13 '18 at 14:47






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
Nov 13 '18 at 14:47














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
Nov 13 '18 at 15:26





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
Nov 13 '18 at 15:26












2 Answers
2






active

oldest

votes


















2














You can open the code in the Editor using,



view(blockHandles(iHandle))





share|improve this answer

























  • And we have a winner! Easy and readable. Thank you! I thought there must exist such a method. PS, Phil you now know where my other question led up to. ;)

    – Smartskaft2
    Nov 16 '18 at 11:34


















0














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.






share|improve this answer























  • This could be the solution to my problem, if I was allowed to make this change. However, this is code used for safety critical functions which makes it too risky. Also, I want this to help my work in an environment where regular MATLAB function blocks are common. I cannot change every model I come in contact with. Thank you for the answer though. It is a method I will have to try out sometime. :)

    – Smartskaft2
    Nov 13 '18 at 15:30










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279284%2fprogrammatically-open-a-simulink-matlab-function-blocks-code%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can open the code in the Editor using,



view(blockHandles(iHandle))





share|improve this answer

























  • And we have a winner! Easy and readable. Thank you! I thought there must exist such a method. PS, Phil you now know where my other question led up to. ;)

    – Smartskaft2
    Nov 16 '18 at 11:34















2














You can open the code in the Editor using,



view(blockHandles(iHandle))





share|improve this answer

























  • And we have a winner! Easy and readable. Thank you! I thought there must exist such a method. PS, Phil you now know where my other question led up to. ;)

    – Smartskaft2
    Nov 16 '18 at 11:34













2












2








2







You can open the code in the Editor using,



view(blockHandles(iHandle))





share|improve this answer















You can open the code in the Editor using,



view(blockHandles(iHandle))






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 16:38

























answered Nov 15 '18 at 16:27









Phil GoddardPhil Goddard

8,9271823




8,9271823












  • And we have a winner! Easy and readable. Thank you! I thought there must exist such a method. PS, Phil you now know where my other question led up to. ;)

    – Smartskaft2
    Nov 16 '18 at 11:34

















  • And we have a winner! Easy and readable. Thank you! I thought there must exist such a method. PS, Phil you now know where my other question led up to. ;)

    – Smartskaft2
    Nov 16 '18 at 11:34
















And we have a winner! Easy and readable. Thank you! I thought there must exist such a method. PS, Phil you now know where my other question led up to. ;)

– Smartskaft2
Nov 16 '18 at 11:34





And we have a winner! Easy and readable. Thank you! I thought there must exist such a method. PS, Phil you now know where my other question led up to. ;)

– Smartskaft2
Nov 16 '18 at 11:34













0














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.






share|improve this answer























  • This could be the solution to my problem, if I was allowed to make this change. However, this is code used for safety critical functions which makes it too risky. Also, I want this to help my work in an environment where regular MATLAB function blocks are common. I cannot change every model I come in contact with. Thank you for the answer though. It is a method I will have to try out sometime. :)

    – Smartskaft2
    Nov 13 '18 at 15:30















0














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.






share|improve this answer























  • This could be the solution to my problem, if I was allowed to make this change. However, this is code used for safety critical functions which makes it too risky. Also, I want this to help my work in an environment where regular MATLAB function blocks are common. I cannot change every model I come in contact with. Thank you for the answer though. It is a method I will have to try out sometime. :)

    – Smartskaft2
    Nov 13 '18 at 15:30













0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 12:35









rinkertrinkert

1,444517




1,444517












  • This could be the solution to my problem, if I was allowed to make this change. However, this is code used for safety critical functions which makes it too risky. Also, I want this to help my work in an environment where regular MATLAB function blocks are common. I cannot change every model I come in contact with. Thank you for the answer though. It is a method I will have to try out sometime. :)

    – Smartskaft2
    Nov 13 '18 at 15:30

















  • This could be the solution to my problem, if I was allowed to make this change. However, this is code used for safety critical functions which makes it too risky. Also, I want this to help my work in an environment where regular MATLAB function blocks are common. I cannot change every model I come in contact with. Thank you for the answer though. It is a method I will have to try out sometime. :)

    – Smartskaft2
    Nov 13 '18 at 15:30
















This could be the solution to my problem, if I was allowed to make this change. However, this is code used for safety critical functions which makes it too risky. Also, I want this to help my work in an environment where regular MATLAB function blocks are common. I cannot change every model I come in contact with. Thank you for the answer though. It is a method I will have to try out sometime. :)

– Smartskaft2
Nov 13 '18 at 15:30





This could be the solution to my problem, if I was allowed to make this change. However, this is code used for safety critical functions which makes it too risky. Also, I want this to help my work in an environment where regular MATLAB function blocks are common. I cannot change every model I come in contact with. Thank you for the answer though. It is a method I will have to try out sometime. :)

– Smartskaft2
Nov 13 '18 at 15:30

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279284%2fprogrammatically-open-a-simulink-matlab-function-blocks-code%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20