Ho we run a section code one time in Matlab function block ? In below body I dont want assigning zeros in Dout after 1st iteration. This matlab function block run 100 times and problem is it every time set zeros in dout.
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Sarfaraz Ahmed
il 2 Ott 2018
Modificato: Sarfaraz Ahmed
il 6 Ott 2018
function y = ADC(u, clk, count)
%function y = fcn(u, clk)
count=count+1;
% if clk==1
% disp('hello');
% end
% emulation of SAR ADC operation
Nbit = 7;
Vref = 64;
VCM = Vref/2;
%dVin = (-37298:37299)/37299*Vref;
Point=u;
dVin = (-Point:Point)/Point*Vref;
%plot(dVin);
Vip = dVin/2+VCM;
Vin = -dVin/2+VCM;
N = length(dVin);
k=count;
*Dout = zeros(1,N);
B = zeros(1,Nbit);*
while k <=N
if clk==1
Vxp = Vip(k);
Vxn = Vin(k);
for kbit = 1:Nbit
if Vxp - Vxn > 0
B(kbit) = 1;
Vxp = Vxp - Vref*2^(-kbit);
else
B(kbit) = 0;
Vxn = Vxn - Vref*2^(-kbit);
end
end
Dout(k) = B(1)*64 + B(2)*32 + B(3)*16 + B(4)*8 + B(5)*4 + B(6)*2 + B(7)*1 -64 +0.5;
% k=k+1;
break;
else
%k=k+1;
break
end
end
plot(dVin,Dout,'r*')
y = Dout;
0 Commenti
Risposta accettata
Honglei Chen
il 2 Ott 2018
You can make Dout persistent so it preserves the value between runs, like this
persistent Dout;
if isempty(Dout)
Dout = zeros(1,N);
end
HTH
14 Commenti
Walter Roberson
il 5 Ott 2018
You should be using a triggered subsystem if the code is not to run at all when the control signal is 0.
Note: in such a case you would no longer need clk1 as an input, as the reconfigured block would only get run when clk1 was 1.
Più risposte (1)
Honglei Chen
il 5 Ott 2018
Do you get an error? What is the error message? Looks like you may have some dimension issues? An alternative way is to control the calling of this block, like in a subsystem so it does not get triggered when clock is 0.
HTH
3 Commenti
Walter Roberson
il 6 Ott 2018
The clock is a signal. You can put a delay block on the signal. Or you could use a pulse generator such as https://www.mathworks.com/help/simulink/slref/pulsegenerator.html
Vedere anche
Categorie
Scopri di più su String in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!