How can I write a Mask parameter to the workspace in Simulink?

Hello world!
I have a masked subsystem with two parameters fc_lo and fc_hi. They define the corner frequencies of a Bandpass FIR filter inside the subsystem (specified in the dialog box of the Bandpass FIR filter block). However, once run, I get an error saying how these variables are not defined in the Matlab workspace.
Is there a way to write these Mask parameters to the matlab workspace so that the bandpass filter block will recognize them?
(I already looked at previous questions such as https://uk.mathworks.com/matlabcentral/answers/179344-how-can-i-mask-a-bandpass-filter-block-from-the-dsp-system-toolbox-r2014b?s_tid=answers_rc1-1_p1_MLT - but when I implemented their solutions the model ran incredibly slow).
Thanks
Rafael

2 Comments

Hi Rafael,
You have to initialize the value of parameter used by the blocks inside subsystem using fc_lo and fc_hi.
Could you please share your screenshot of subsystem block diagram, so I can assist you further.
Hello Ashish,
This is the masked subsystem:
The mask is the following:
Inside, the subsystem is the following. It is essentially a "filtfilt" operation (i.e. zero-phase filtering by reversing the input signal and filtering again). Bandpass filter 1 and 2 are identical and their parameters are also shown.
So how exactly do I go about "initializing the value of parameter used by the blocks inside subsystem using fc_lo and fc_hi"?
Thanks

Sign in to comment.

 Accepted Answer

The masked parameters seem to mismatch internal variables. The easiest way is to change the #1 and #2 parameter name in the mask dialog from "ecg_fc_lo" to "fc_lo", from "ecg_fc_hi" to "fc_hi". But then, you also need to add the masked parameter for "fs".
Look in document to learn about mask.

11 Comments

Sorry, I forgot to clean up those parameters. They should have indeed been fc_lo and fc_hi as is expected in the bandpass blocks. Ill correct that in the previous post.
Even, when I change them to fc_lo and fc_hi the problem is the same: fc_lo and fc_hi are not defined in the workspace and I therefore get the following error:
Error evaluating 'InitFcn' callback of Bandpass Filter block (mask) 'FIRfiltfilt/Filtfiltering/Bandpass Filter 1'. Callback string is 'fdesignblkfcn(gcbh, {'InitFcn'});'
Caused by:
  • Invalid design The variable fc_lo-2 is not defined in the MATLAB workspace.
Component:Simulink | Category:Model error
fs is already defined in the workspace so I do not need to add the mask parameter.
It might be easier if you post your Simulink model and indicate the MATLAB version.
Matlab 2019b.
I've attached the model and all the matlab workspace variables.
The problem is fc_lo and fc_hi. I do not want to define them in workspace - I want to be able to do this in the mask of the attached subsystem model.
The Bandpass block is already a masked block. It assumes the masked variables all come from MATLAB base workspace. This is not a good design but it may have to be like this because many of the callbacks need to know the value right away. Put a break point in the M-file evaluatevars.m and run your simulation, you will see it. It tries to call evalin('base',...);
As long as you have fc_lo and fc_hi defined in the baseworkspace, the model simulates.
This block helps you design a filter on-the-fly. The "InitFcn" callback fdesignblkfcn.m is called whenever the dialog of the Bandpass block is changed. It doesn't seem to fit to add another layer of mask.
Thanks for looking into it.
Yes, in fact I tried to add another layer of mask layer immediately above just to define fc_lo and fc_hi but that didn't work. It gave the same error.
So then, from what I understand, there's no obvious way to initialize fc_lo and fc_hi from the mask parameters to the workspace before the bandpass filters are executed?
You just want to mask the Bandpass block to avoid setting up so many parameters.
Leave your Bandpass block parameters as it is. You could just add a graphic to your subsystem. In other words, mask you subsystem without any variables or dialog.
In somewhere else, add a dumy block (e.g. empty subsystem block) and mask it with two variables, fc_lo and fc_hi. Then in the mask callback, do assignin('base','fc_lo',WhatEverValueFromMask). This is to assign variable 'fc_lo' in base workspace to work around the limitation of the Bandpass block. When simulation starts to run, the "InitFcn" of the Bandpass block will find 'fc_lo' and 'fc_hi' in base workspace and update the filter design.
Thanks again, Im going to try this.
Two quick questions re: this:
1) Should I by doing the 'assignin('base','fc_lo',WhatEverValueFromMask)' int he dummy/empty subsystem mask or the filtfiltering bandpass subsystem block?
2) How exactly do I edit the mask callback? Is it in the 'initialiation' tab of the mask? Where can I read up on what exactly this callback does/is?
Thanks so much!
EDIT: Spoke too soon - still not working (same error - fc_lo not defined in workspace). Not sure where exactly Im supposed to do the 'assignin('base','fc_lo',WhatEverValueFromMask)'.
I've attached my new model. As you can see, Im doing 'assignin(...)' in the mask initialization tab of the dummy block.
Its weird, I'm not sure why but it seems like on some runs the paremeters get sent to the workspace and the model works fine, but other runs they do not get sent. I cant seem to figure it out but I think I'm almost there.
Hi Rafael,
While adding variables to the parameter make sure the variables are visible, when you type as parameter of block you should get a suggestion, so are you able to see the variables as suggestions when you type it in the block parameters value?
Sorry for the extra route. You could utilize your original mask. In the original model you posted, in the mask of the subsystem, in the initilization tab, add the two lines below
assignin('base','fc_lo',fc_lo);
assignin('base','fc_hi',fc_hi);
You should have fc_lo and fc_hi defined in the base workspace initially to avoid error when starting simulation right after openning the model. The value doesn't matter. When Simulation runs, the value got updated.
I think I understand what is happening.
I did what Fangjun suggested and it works. I just have to be careful HOW I do it.
I was testing the model sent above (FIRfiltfilt3) by deleting the workspace variables fc_lo and fc_hi then re-running the simulink model. This did not work because the mask parameters are sent to the workspace via 'assignin('base','fc_lo',WhatEverValueFromMask)' ONLY a) when the model is opened and b) when the paramters are changed in the mask.
What I was doing was opening the model, running it (it would work the first time), then deleting the variables in the workspace and trying to run it again (this would not work because the variables were not being created in the workspace in this fashion).
However, if I delete the workspace variables and then go back to the mask and change fc_lo and fc_hi, they are once again created in the workspace and the model runs fine.
This is just stuff to consider but it works fine, here is the final model for anyone interested!
Thanks so much for your inputs!

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!