Following function works as .m file but does not produce any output as function file
2 views (last 30 days)
Show older comments
Mehdi Gh
on 3 May 2016
Commented: Walter Roberson
on 4 May 2016
Hello,
The following code works prefectly when run as .m file but when I want to use it as a function its output is empty!
function timespan= timesep(begtime,endtime,stinterval,btinterval,sindent,bindent)
% clearvars
% begtime=0;
% endtime=115;%
% stinterval = 5;%
% btinterval=20;%
% sindent = 1;%
% bindent = 5;%
time_span = [begtime]; end_span = 0;
for iter = 1:fix(endtime/btinterval)
timing = [end_span+begtime+sindent:sindent:end_span+stinterval end_span+stinterval+bindent:bindent:end_span+btinterval]';
time_span = [time_span; timing]; % (1:end-1)
end_span = iter*btinterval;
end
if mod(endtime,btinterval)
timing = [end_span+begtime+sindent:sindent:end_span+stinterval end_span+stinterval+bindent:bindent:endtime]';
time_span = [time_span; timing];
end
timespan = time_span;
end
To use it as function I run
tspan=timesep(0,200,1,5,5,20);
but tspan is empty!
Explanation of what the code does: It takes a range and divide it to different intervals. however the intervals are smaller at specific ranges and bigger at the rest. This is like having different time-steps when solving a numerical problem where at some points due to numerical instabilities time-steps should be very small, but at other points can be big to increase the computational speed.
3 Comments
Walter Roberson
on 4 May 2016
Mehdi Gh comments "Thank you. you were right. the order in my function call was wrong!"
Accepted Answer
Walter Roberson
on 4 May 2016
Your intervals are too small. In
timing = [end_span+begtime+sindent:sindent:end_span+stinterval end_span+stinterval+bindent:bindent:end_span+btinterval]';
begtime+sident > stinterval and stinterval+bindent > btinterval so no matter how far along you shift the end_span, the end point is before the beginning point and so the interval is empty.
2 Comments
More Answers (1)
Stalin Samuel
on 3 May 2016
The input parameters highly influence the output.So try with different input values .then you can understand the exact issue.
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!