Errors occurred during parsing of MATLAB function

I am trying to change the phase angle of the three phase source during simulation time. For this I intend on using a function block. Can you validate whether the application is correct. I am getting an error. I am not sure whether I am using the simulation time correctly. And do I need to add a line in matlab main script too?
I have used the following code in Matlab function block
function fcn(t)
coder.extrinsic('set_param');
coder.extrinsic('num2str');
phase=get_param('threebus/Three-Phase Source', 'PhaseAngle');
if (t>=5) && (t<=6);
phase2 = phase+1;
set_param('threebus/Three-Phase Source', 'PhaseAngle', num2str(phase2)); % write the name of your simulink model/block name
else
phase=0;
end

6 commentaires

You are configured to sample at 10 kHz.
Do I understand correctly that once you reach 5 seconds into the simulation, that 10000 times per second you want to increase the phase angle by 1, ending at 6 seconds? So that at 6+1/10000 seconds, the phase angle will be 10001 more than it was at 5-1/10000 seconds??
The initial value of phase angle is zero. I want to increase it by 1, for (t>=5) && (t<=6), the total simulation time is 20 seconds, sampled at 10kHz. I have shared the detail of the error
You failed to coder.extrinsic('get_param')
That caused the call to get_param to be considered to fail, which caused Simulink to consider that phase was not being assigned to. With phase not being considered to be assigned to, phase is not considered to exist in order to be able to use it to calculate phase2, so phase2 is considered to fail as well.
Every time the function is triggered you read from threebus/Three-Phase Source/PhaseAngle, and if the time is from 5 to 6 you add 1 to the phase and write the result to threebus/Three-Phase Source/PhaseAngle .
So exactly 5 seconds rolls around. Suppose the phase angle stored was 3 at that point. You add 1 to it, store the 4.
5+0.0001 seconds rolls around -- because you are using discrete sampling of 0.0001. The function gets triggered again (function is just sampling the clock, not being triggered by some subsystem, so it gets invoked every new clock cycle). You retrieve the 4, check and find that 5.0001 is >=5 and <= 6, so you add 1 getting 5 and store the 5 back.
5.0002, you retrieve the 5, add 1, store the 6
5.0003, you retrieve the 6, add 1, store the 7...
I would suggest to you that you do not use this approach at all.
Use a relational operator block with Clock input, compare >= 5, double output. This generates 1 for >= 5
Use a relational operator block with Clock input, compare > 6, double output. This generates 1 for > 6.
Subtract the above two with a math operator. This gives 0 until 5, 1 until (and including) 6, 0 for t > 6 .
Add the result to the phase angle block.
No MATLAB Function blocks needed. No memory needed.
Thankyou for your response. It was helpful and I have tried implementing this. I am getting an error, it would be great if you could point out what am I doing wrong here. (note: I changed the time to t>=10 and t<11)
I implemented the following logic on simulink and added the following lines in the matlab script:
ph_a= get_param('threebus/Three-Phase Source', 'PhaseAngle');
ph_new=set_param('threebus/Three-Phase Source', 'PhaseAngle', str2double(ph_a)+ph_j);
Error in threebusmodel (line 15):
ph_new=set_param('threebus/Three-Phase Source', 'PhaseAngle', str2double(ph_a)+ph_j);
With the logic I proposed, you would not have any set/get at all.
But in any case: the set_param very likely (but not completely certainly) requires that you send it a character vector.
I am not clear at the moment as to whether the get_param is going to return a character vector or a numeric value.
If it returns a character vector then str2double() of that would be fine, and adding a number to the result would be fine, but you would have to num2str() or equivalent for the set_param() call.
... But as I said,you should not need that code at all. Do not change the properties of PhaseAngle, just add the appropriate correction to it before using the value.
The logic you drew would be plausible if ph_new were threebus/Three-Phase Source/PhaseAngle . You could use a GoTo if you are inside the same subsystem. You could use a GoTo in theory between subsystems, but that is typically not a good idea: better would be to have the value as an OutPort and connect the port as appropriate.

Connectez-vous pour commenter.

Réponses (0)

Produits

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by