5

I have a Simulink model, the purpose of which is automated code generation.

My model uses S-functions (developed by another party), which has hard-coded assumptions about the path. For instance, several external data files are needed, which are referenced in the S-function via a relative path like ..\Bin\data\datafile.bin. This makes it necessary to set MATLAB's current working directory to a specific path before the model can be run.

I can automatically check and set the correct path via model callback functions. However, all model callback functions only seem to be related to the simulation process, not the build process. That means that I can run the model irrespective of what directory I'm in, but when I try to build the model, it always fails unless I manually navigate MATLAB back to the correct directory.

Needless to say, that's quite annoying. So I was wondering if there is something like a "preBuildFcn" callback fnuction, a function that is run before starting the build process? Any other solution (that does not involve modifying the S-function) is also very welcome.

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96

2 Answers2

4

There are plenty of hooks into the build process of Simulink / Embedded Coder ('entry', 'before_tlc', 'after_tlc', 'before_make', 'after_make', 'exit', and 'error'). I assume you want an 'entry' hook.

All you need to do is write an M-function with the name your_system_target_file name_make_rtw_hook, as explained in the documentation Customize Build Process with STF_make_rtw_hook File.

In case you can't open the online documentation (login required), here is the path to the HTML in your MATLAB installation: MATLAB root\help\rtw\ug\customizing-the-target-build-process-with-the-stf-make-rtw-hook-file.html

Community
  • 1
  • 1
pmb
  • 866
  • 7
  • 24
  • Oops...spoke too soon: "**Warning: During the 'entry' hook call to 'grt_make_rtw_hook', pwd was changed to** **. **Changing directories is not allowed. The pwd will be reset to** **". (MATLAB 2010a) – Rody Oldenhuis Sep 09 '14 at 13:24
  • Hmmm.. pity. Drop those S-functions ;) – pmb Sep 09 '14 at 14:06
  • I'm not sure if this works, but maybe you can try using the FileGenControl function to point to the directory that you need to change to for your S-Functions. http://www.mathworks.com/help/rtw/ref/simulink.filegencontrol.html – pmb Sep 12 '14 at 08:01
  • I'll accept your answer, as it seems to be rather more like a shortcoming in MATLAB <= 2010a than a shortcoming in your answer or continued support. I'll contact The MathWorks about this; if I learn anything new, I'll post it here. Thanks again for everything. – Rody Oldenhuis Sep 12 '14 at 08:42
1

I am not sure whether building simulink models is sufficiently similar to building regular MATLAB programs, but here is what I used in the past:

  1. Set up the project manually
  2. Build the project programmatically

The program that is used to build the project should be able to set the path or do other custom things.

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
  • From what I've seen of Simulink so far is that Simulink and MATLAB are two different worlds :) Proper documentation on programmatically controlling or building Simulink models is also a lot sparser than what I'm used to in MATLAB, but your solution might be worth a shot. – Rody Oldenhuis Sep 09 '14 at 11:26
  • When you're constructing or debugging a Simulink model, you're doing a lot of graphical work, accelerated by keyboard shortcuts. It'll be a pain of getting used to NOT use Ctrl+B to build the model, but go back to the (usually well-buried) MATLAB command prompt and run the custom build command...Perhaps one can assign key combinations to start shortcuts to MATLAB scripts/functions? – Rody Oldenhuis Sep 09 '14 at 11:28
  • @RodyOldenhuis I don't know how to assign a function to a keyboard shortcut, but I suppose you could use a clickable shortcut. – Dennis Jaheruddin Sep 09 '14 at 11:38
  • Yup, that's what I was thinking. I just did it, and indeed it works, however, I've also worked with it for 15 minutes and it would be *so* much better if you could somehow attach a keyboard shortcut to it... – Rody Oldenhuis Sep 09 '14 at 12:29