Hi,
I have a function that works well when I called it one by one.
>>Calculate(a,cs,sclk)
ans: 20
>>Calculate(a,cs,sclk)
ans: 20.5
>>Calculate(a,cs,sclk)
ans: 20.25
However, when i switching the function inside a for loop, it kept produced the same answer.
for j=1:9
d=Calculate(a,cs,sclk);
fprintf('value %d is %d\n',j,d);
end
Asnwer:
value 1 is 10
value 2 is 10
.
.
.
value 9 is 10
And I called it in a series. They all produced the same answer.
>> d1=Calculate(a,cs,sclk);d2=Calculate(a,cs,sclk);d3=Calculate(a,cs,sclk);d4=Calculate(a,cs,sclk);
Ans:d1=15,d2=15,d3=15,d4=15,d5=15
Anyone know what's happened for my function here?

4 Kommentare

Walter Roberson
Walter Roberson am 17 Dez. 2022
No, no-one knows. We have not seen the code for your function
gdz
gdz am 17 Dez. 2022
How about the time required for calling a function? How can I find the minimum time difference for two calls?
Askic V
Askic V am 17 Dez. 2022
You can measure time with tic and toc.
Please have a look at:
https://www.mathworks.com/help/matlab/ref/tic.html
gdz
gdz am 18 Dez. 2022
Thank you all of your help

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Askic V
Askic V am 17 Dez. 2022
Verschoben: Walter Roberson am 18 Dez. 2022

1 Stimme

For loop will be executed in a matter of milliseconds. On the other hand when you call function once at a time, there is a significantly more time difference between calls. So perhaps your input parameters cannot change fast enough in the loop call.

2 Kommentare

Walter Roberson
Walter Roberson am 17 Dez. 2022
Verschoben: Walter Roberson am 18 Dez. 2022
I agree with Askic V
gdz
gdz am 17 Dez. 2022
Verschoben: Walter Roberson am 18 Dez. 2022
I have add a pause function inside and it works!!!! Thank you very much.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2020a

Gefragt:

gdz
am 17 Dez. 2022

Verschoben:

am 18 Dez. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by