Simulink Dynamic Popup Parameter Values Set at Mask Initialization
조회 수: 14 (최근 30일)
이전 댓글 표시
I am trying to populate an S-Block Simulink Mask's parameter with a list of files from a specific directory. It seemed to me the best way to do this would be to set up the parameter as a 'popup' type, and give it some acceptable initial values in the 'popups' type-specfic options, and then use the Mask Initialization code to overwrite these with the full list.
The code I am currently trying to use looks like this:
directory_to_list = what('Some Directory on the Search Path');
p = Simulink.Mask.get(gcb);
a = p.Parameters(13); %note it is the 13th parameter of this mask
a.set('TypeOptions', directory.mat); %drop in a cell array listing of .mat files
This produces the error message:
Error in 'blockname'. Initialization commands cannot be evaluated.
Caused by:
Invalid inputs specified for method 'set'. Attempt to modify mask parameter name of block 'blockname' in its Mask Initialization. Changing mask parameter name as part of MaskInitialization is not allowed.
I've replaced my blockname with the dummy name 'blockname' above to keep the issue generic.
I tried some additional things such as:
directory_to_list = what('Some Directory on the Search Path');
p = Simulink.Mask.get(gcb);
a = p.Parameters(13); %note it is the 13th parameter of this mask
a.set('Name', 'same name of block', 'TypeOptions', {'red', 'blue'});
and even a simple test like:
p = Simulink.Mask.get(gcb);
a = p.Parameters(13); %note it is the 13th parameter of this mask
a.set('Visibility', 'off');
and I always get the same error message. Clearly either I am making some important mistake, or this is not an allowed way to change the popup's option list. Is there a better way to do this?
Finally, as I'm sure this question will come up, I have checked the "Allow library block to modify its contents" box on the initialization tab.
Thoughts? Suggestions? Anything would be helpful.
댓글 수: 1
답변 (1개)
John Barber
2014년 9월 16일
Here are my results (in R2014a) on a block with parameter 2 being the popup of interest, using a number of approaches:
% Common header to get references to the mask and the mask parameters
msk = Simulink.Mask.get(gcb);
p = msk.Parameters;
% Method 1
p(2).TypeOptions = {'Opt1','Opt2','Opt3'};
% Result: works
% Method 2 - Alternate syntax
p(2).set('TypeOptions',{'Opt1','Opt2','Opt3'});
% Result: works
% Method 3 - Use a cell array variable
myCell = {'a','b','c','d'};
p(2).TypeOptions = myCell;
% Result: works
% Method 4 - Try using a variable from the base workspace
myCell = evalin('base','someCellVar');
fprintf(1,'The class of the ''myCell'' variable is: %s\n',class(myCell));
p(2).TypeOptions = myCell;
% Result: fails with an error that the value must be of class 'cell', even when it _is_ a cell.
% Method 5 - Append the base workspace values to a cell defined in the mask initialization
myCell = {'C1'};
myCell = [myCell evalin('base','someCellVar')];
p(2).TypeOptions = myCell;
% Result: works
The failure of Method 4 is interesting. Even when the value assigned to myCell is a valid cell array, Simulink throws an error. Possibly there is some issue with the masks initialization code checking the new value's class before the code runs, which would explain why method 5 works.
댓글 수: 1
Jorhabib
2014년 12월 9일
편집: Jorhabib
2014년 12월 9일
Hello there,
Nice detailed answer. I would like however to put this code in my OpenFcn, but still having the option to open the parameters mask by double-clicking (Once you put code in the OpenFcn callback, the params mask you get by double-clicking does not show up unless you right-click the mask and choose Mask > Mask Parameters), so that the user can choose from my dynamically populated list. Any hints on this?
Thanks
참고 항목
카테고리
Help Center 및 File Exchange에서 Author Block Masks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!