value that increase along with changing value by manual switch block simulink using embedded matlab function

hi all
i just wanna ask,how we do the increasing value by changing manual switch in simulink??
for example: i have manual switch, with 2 constant block connect to it, first signal= 1 , second=0. i have calculation that everytime we change value in manual switch the system will add 10 to the current signal.
how we do it with embedded matlab function??

1 Comment

Duplicate is http://www.mathworks.com/matlabcentral/answers/11705-value-that-increase-along-with-changing-value-in-manual-switch-block-simulink

Sign in to comment.

 Accepted Answer

Do you have to use EML? If not, it could be done easily in Simulink using a trigger subsystem. Type "sldemo_counters" to open the demo model, connect your manual switch block output to the triggered subsystem. Inside the system, change the trigger from "risgin" to "either", change the constant from 1 to 10 then you got it.

More Answers (4)

Please do not open duplicate questions; it just ends up duplicating efforts.
You can edit your previous question if needed.
FWIW, here's an EML implementation:
function y = mycounter(u)
%#eml
persistent count;
persistent prevInput;
firstInput = false;
if isempty(count)
count = 0;
end
if isempty(prevInput)
prevInput = 0;
firstInput = true;
end
if ~firstInput
if (prevInput ~= u)
count=count+1;
end
end
prevInput = u;
y = count;
end
Okay, feed the switch output signal to the triggered subsystem, still choose "either". Also, feed the switch output signal to an Inport of the the triggered subsystem, multiple it by 10, then feed it to an Integrator block. Did not verify. Should work. Try it yourself.

3 Comments

error fang, it was:
'untitled1/Triggered Subsystem/Integrator' has a continuous sample time. Only constant (inf) or inherited (-1) sample times are allowed in triggered subsystem 'untitled1/Triggered Subsystem'.
anyway, im using slider gain with value -1,0,1
Then change the sample time of the Integrator block to be -1.
not integrator fang, but unit delay.. thanks fang!! :)

Sign in to comment.

what if i have 3 signal fang??? -1,0,1 ,, if -1 then the current value was be diminished, 0 the value stay still and 1 the value increase.

4 Comments

It certainly can be done. It is different though. Your original intention was to increase the counter by 10 every rising or falling edge of your switch output. This one, how do you increase if the switch output is 1? Increase 10 at every time step? Or there is something else trigger the increase?
nope, only 1 trigger, if 1 then current value add by 10, if 0 then current value stay still, if -1 then current value minus by 10.
how is it fang???
If 1 then current value is increased by 10. Increased only once, or every time step as long as the switch output is 1?
no only once, the case was the same with the manual switch, but in this case there was 0 and -1 for minus it.
so when curent value is 10, then the signal give 1, then 10 + 10 = 20,, and then the signal gives 0, then 20 + 0 = 20,, and if the signal gives -1, then 20 - 10 = 10,,
so now, the current value is 10.
how is it fang???

Sign in to comment.

Categories

Find more on Simulink in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!