Global Variable To Be Used Inside Simulink S-Function

20 Ansichten (letzte 30 Tage)
Patrick Harrington
Patrick Harrington am 4 Nov. 2016
Hello,
I am trying to understand how to leverage a global variable to hold a string that is to be used in an S-function as soon as my model is ran. Basically, I want to be able to run things in batches down the road, and one of the things that needs to happen in my simulink model is that a user provides a file name 'foo.txt' to draw data from and a destination file name where data is streamed out to such as 'bar.txt'.
These values are to be set once before the sim runs. In my workspace I declared a global variable called input_file and set that to 'foo.txt'. When I run the simulink model with a breakpoint at the point in the DoPostPropSetup function where I perform a task involving the global variable, I expected to see it in the workspace for that function. That was not the case. I even declared global input_file right before the breakpoint and it did not appear in my workspace, much less carry the value I wanted.
I also tried using the "constant" block to carry the input file name, but what I found was that I can't use a string for that, (okay, so convert it using uint8(input_file)), and much worse, if I set up an input with the length of the file name hard coded (not desirable) as the dimension of the InputPort and then use a breakpoint in the S-function block I've made, the value for the InputPort is simply ' ' (two single quotes; an empty value). Do I have to wait until Simulink samples this input?! I need the filename at t=0 to begin the sim, not t = length(input_file)/Fs.
Is there a way to do what I want here? I need the user of my simulink model to be able to set an input and output file name which is to be used in a part of an S-function that acts on that string. Setting globals up in the workspace doesn't seem to work, and if I can't get the whole array into the S-function at t=0 to be acted on using a "constant" block, then I'm stuck.
Thank you in advance for your advice!

Antworten (1)

Walter Roberson
Walter Roberson am 5 Nov. 2016
  3 Kommentare
Walter Roberson
Walter Roberson am 7 Nov. 2016
When I was searching the other day I was not sure if such registry blocks would be appropriate for S functions or if they only applied to MATLAB Function Blocks
Patrick Harrington
Patrick Harrington am 7 Nov. 2016
Bearbeitet: Patrick Harrington am 7 Nov. 2016
So I am trying to have the output from an S-function write to a data store, but I'm getting a very familiar and frustrating error: "Cannot access data in (block). The probably cause of this error is accessing data that is available only when the simulation is running".
I'd love to find a more elegant way of saving off per-simulation data, but I'm not really sure how to address this error, which appears anytime I use the work vectors, the inputs or the outputs to an S-function.
Attempt: I created a work vector in DoPostPropSetup, and then try to save the data in a work vector, but in the same function I wind up with the same "cannot access data" error. I'm so lost; are work vectors only to be used with the input/output or other work vectors ... seems like I'm forced to an intermediate step for anything I'd like to save and use throughout an S-function, or at all, namely, by writing it to a file.
Another Attempt: I have modified my little S-function to just dump out a timeseries wholesale to the output. I no longer get the error about not observing data, but that might be because the new error I'm getting is obscuring it. I want to load length(dlmread('file.txt')) into the data store block. I have my S-function size its work vector and output vector to have this width, but I'm not sure I'm able to get the input to the Data Store Write block to match this. So, my follow-up question is: is it possible to re-size the dimensions of the Data Store block?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Model, Block, and Port Callbacks finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by