How can I calculate block parameters from mask parameters?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
The Goal:
I have a subsystem which I now want to control over a mask. The mask should allow me to chose between two materials and give a thickness and radius of a cylinder. In the subsystem I then want to use the material choice to define the relevant material parameters (e.g. in a "if material == 1, then parameters =... " statement) and I want to pass on the thickness and radius, however they are not direct parameters for a block in the code (so I can't just promote them). Instead I want to do calculations with them, e.g. I have an impedance that calculates from Z=rho*pi*r^2. Rho should be defined by the Material choice and r should be given in the mask in an edit block. See below for what I picture for the mask:

Now what I need to calculate from these is some further values. Before I created the subsystem I did this in the InitFunc of the model.
What I have currently:
classdef piezoElement_mask
methods(Static)
% Following properties of 'maskInitContext' are available to use:
% - BlockHandle
% - MaskObject
% - MaskWorkspace: Use get/set APIs to work with mask workspace.
function MaskInitialization(~)
end
% Use the code browser on the left to add the callbacks.
function material(mat)
if isequal(mat, 1)
g33=0.0142188;
g31=-0.0333414;
c33D=1.362e11;
c11D=7.268e10;
h33=g33*c33D;
h31=g31*c11D;
eps33S=9.249e-9;
rho=14672.908;
end
if isequal(mat,2)
c33D=1.295e11;
c11D=8.587e10;
h33=1.302e9;
h31=-1.684e9;
eps33S=8.711e-9;
rho=17435.538;
end
end
function thickness(th)
d = th*1e-3;
end
function radius(rd)
r = rd*1e-3;
A=pi*r^2;
C0=A*eps33S/d;
Z33 = A*sqrt(c33D/rho)*rho;
t33=d/sqrt(c33D/rho) ;
Z11 = 2*pi*d*r*sqrt(c11D/rho)*rho;
t11=2*r/sqrt(c11D/rho) ;
end
end
end
There's probably a better way to do this, also this right now doesn't work. I always get an error for thickness and radius (material seems to be fine?) and my blocks in the subsystem still use the parameters stored in the MATLAB variable browser, not the updated values I try to give here.

Can anyone help? I've found loads of information on passing on parameters, but only for when you use the parameter directly and not if you want to use them for calculations first.
0 commentaires
Réponse acceptée
Angelo Yeo
le 22 Nov 2023
Modifié(e) : Angelo Yeo
le 22 Nov 2023
If you were to choose one our of multiple scenarios depending on your choices, a good way to go is to use a variant subsystem with masking. The example below can be of your help.
8 commentaires
Angelo Yeo
le 23 Nov 2023
Thank you Annika for your reply. I'm glad you made some progress :D
BLOT: Change the code like below.
function rad(~)
radius = get_param(gcbh, 'rad');
radius = str2double(radius); % Add this %
disp(radius)
disp(radius-48)
assignin('base', 'r', radius*0.001);
end
Note that the variable radius is a character variable. I believe you must have taken this value from "Edit" field. Characters are basically numbers and mapped by ASCII rule. So, MATLAB dynamically mapped back the character into original number. For example, the character 'A', 'B', 'C' are mapped from 65, 66, 65 respectively.
double('A')
double('B')
double('C')
Accordingly, the character '3' is mapped from is mapped from 51.
double('3')
That's why you would get the result 3 when you calculated '3' - 48.
'3'-48
Same story for '2.5'. Let's see how '2', '.', '5' are mapped from 50, 46, 53 in ASCII respectively.
double('2.5')
So that's why
'2.5' - 48
So, the solution is to use str2double. This will change characters into the number (not mapping back to numbers based on ASCII rule).
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Subsystems dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!