How can I have the To File block use the file name from a workspace variable in Simulink 6.5 (R2006b)?

18 views (last 30 days)
I have a To File block in my model and I want to use a workspace variable containing a string to specify the file name for this block. The To File block does not accept the name of a workspace variable like the Gain block does.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The To File block does not have the ability to evaluate a workspace variable to get the file name. In this case you can mask the block and call the SET_PARAM function from the mask's initialization callback function. Here are the steps:
1. Select the To File block in your model
2. In the Command Window enter
set_param(gcb,'Mask','on');
3. Right-click the block and choose "Edit mask" from the context menu.
4. On the Parameters pane in the Mask Editor add a parameter to store the file name entered by the user
5. On the Initialization pane add a call to SET_PARAM to set the To File block's filename property:
set_param(gcb,'Filename',fn);
6. You can now enter the filename as string into the block mask or you can enter the name of a variable which is then evaluated
Please find attached an example model. The steps mentioned above could also be used for the following blocks:
- To Workspace
- From Workspace
- To File
- From File
  3 Comments
Kristia Harris
Kristia Harris on 1 May 2015
I seem to be having the same problem with the "From File" block. I have the prompt and variable set as "Filename" and "Input_Filename" under the Parameters tab. The initialization command is set as set_param(gcb,'Filename',Input_Filename); In the Workspace, the Input_Filename = 'code.m'
Any ideas here?
Thanks,
Kris

Sign in to comment.

More Answers (1)

Marc Compere
Marc Compere on 18 Jan 2015
Nice. Using a mask to evaluate the filename is clever.
mask_example.mdl worked correctly with no modifications in R2014a.
I've attached a modified version that sets the filename to the computer's current date and time. This creates a (probably) unique filename each time the simulink model is started. As long as you don't press start more than once a second it'll create a filename with DD.MMM.YYYY.HH.MM.SS in the filename. This is handy for collecting data during testing.
I would upload the .slx directly but the uploader won't accept it. zipfile attached.

Products


Release

R2006b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by