Filter löschen
Filter löschen

Following function works as .m file but does not produce any output as function file

2 Ansichten (letzte 30 Tage)
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 Kommentare
Mehdi Gh
Mehdi Gh am 4 Mai 2016
There is basically no difference between the two examples. I specially solved the easier case, to see if the code does what it is supposed to. I now changed the values in code to what it is for executing function at command line.
I guess there is something related to calling or retrieving a value from a function which I am not doing correct here. The code runs correctly and generates what I need as .m file, but not as a function file!
Walter Roberson
Walter Roberson am 4 Mai 2016
Mehdi Gh comments "Thank you. you were right. the order in my function call was wrong!"

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 4 Mai 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 Kommentare
Mehdi Gh
Mehdi Gh am 4 Mai 2016
Bearbeitet: Mehdi Gh am 4 Mai 2016
This code works perfectly when I comment the first and the last line (basically change it from function to a .m file) and un-comment lines 3-9. I get what I need there in timespan variable, no problem in it. The problem emerges when I change it to function and call it from the command line. I'm not then able to get that variable (timespan) as the output of function!
Mehdi Gh
Mehdi Gh am 4 Mai 2016
Bearbeitet: Mehdi Gh am 4 Mai 2016
example=timesep(0,115,5,20,1,5);
was the correct way to call the function. I just noticed that I put them in a wrong order when calling the function! Thank you for the help :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Stalin Samuel
Stalin Samuel am 3 Mai 2016
The input parameters highly influence the output.So try with different input values .then you can understand the exact issue.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by