I have this function:
function example(y)
global TICTOC;
tic
TICTOC=5;
toc
end
and I expect TICTOC=5 change the result of toc, since TICTOC is a global variable in tic and toc functions; but this is not the case; Does anyone know the reason?
I like to know the answer, because I 'm worried to declare a global variable, which it's name has been declared global in some other functions, I'm not aware of.
I saw this function in matlab 2008b help
function tic
% TIC Start a stopwatch timer.
% TIC; any stuff; TOC
% prints the time required.
% See also: TOC, CLOCK.
global TICTOC
TICTOC = clock;
function t = toc
% TOC Read the stopwatch timer.
% TOC prints the elapsed time since TIC was used.
% t = TOC; saves elapsed time in t, does not print.
% See also: TIC, ETIME.
global TICTOC
if nargout < 1
elapsed_time = etime(clock, TICTOC)
else
t = etime(clock, TICTOC);
end
thanks.
I think you can use assignin command to send the TICTOC value to base and thus changing the global value.
I use the assignin command to send parameters from function to Base.
Regards
Dilip
I don't know why but I the answer of my question is No. I checked it and it seems that it is not being overwritten. The reason must be because tic, toc is a built in Matlab function
Related
I am collecting data from a potentiometer connected to an Arduino. In the script, I tell matlab to keep collecting data for 2 minutes. But I need to tell it that if the user does not move the potentiometer for 10 consecutive seconds, then it should stop the loop and move to the next session (write the data to an excel file). Does anybody have ideas on how to achieve this?
Probably tic and toc can help you.
tic starts a stopwatch timer. The function records the internal time at execution of the tic command.
toc reads the elapsed time from the stopwatch timer started by the tic function.
tic;
while toc < 10
% Do your loopy things
if variable_changed
tic; % Restart stopwatch
end
end
Furthermore to be sure tic won't interact with other processes you should store it's value like this:
% First start stopwatch
time_since_last_movement = tic;
while toc(time_since_last_movement) < 10
% Do your loopy things
if variable_changed
time_since_last_movement = tic; % Restart stopwatch
end
end
I'm new to MATLAB and I've got a question to ask. I am currently doing up this UI which includes a code which is shown below.
% --- Executes on button press in Start.
function Start_Callback(hObject, eventdata, handles)
% hObject handle to Start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global s brightness a t;
t = 0.1
a = 1;
readasync(s);
while (a == 1)
brightness = str2double(fscanf(s, '%s', 8));
set(handles.brightness, 'String', num2str(brightness));
disp(brightness);
if brightness < 87
if brightness < 44
fprintf(s, '%s',1);
else
fprintf(s,'%s',2);
end
else
if brightness < 130
fprintf(s, '%s',3);
else
fprintf(s, '%s',4);
end
end
if(a==0)
break;
end
pause(1/10);
end
As you can see, I am currently using a pause function to delay the while loop. However my mentor in college suggested that I use tic toc instead of pause to delay the loop. I do not know how should I go about with it. He has given me this function but I do not know how to implement it. Any advice?
function delay(t)
tic;
while toc < t
end
end
Where you have pause(1/10), just write delay(1/10) instead. They are not quite the same thing, as pause has a side effect of flushing any graphics updates and GUI callbacks that may have queued up.
As excaza indicated you can replace the line pause(1/10); by delay(1/10); but the function delay must be visible to where it is being called (so for example you can create a file in the same directory called delay.m that contains the code from your mentor).
It might be helpful to point out that replacing the code pause(1/10); by tic; while toc < 1/10 end; accomplishes the same thing functionally, even though this isn't as good from a coding practice/readability perspective.
how can I compare the execution time of two different methods simultaneously in MatLab?
actually I used "tic-toc" but I'm not sure if I did it in a correct way or not.
this is how I do it:
clc;
clear all;
A=rand(10);
B=rand(50;
tID1=tic;
y1=function1(A,B);
t1=toc(tID1);
tID2=tic;
y2=function2(A,B);
t2=toc(tID2);
Use matlab's built in profiler for better understanding of your code's run-time bottlenecks.
profile clear; %// reset profiler's history
profile on; % start recording
y1 = function1( A, B ); % your code here...
y2 = function2( A, B );
profile off; % stop recording
profile viewer; % visualize the results.
Enjoy!
Yes. That is correct. You don't even need to define tID1 and tID2 the way you are using it. So you can also do:
clc;
clear all;
A=rand(10);
B=rand(50;
tic;
y1=function1(A,B);
t1=toc;
tic;
y2=function2(A,B);
t2=toc;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have wrote a program which it has two functions. The first one's output is a matrix like:
Z = [1 2 3]
The second function should use the output of the previous one. But it can't read the matrix Z.
Can you help please?
My first function is something like this:
function ff = firststep(z)
sigme1=z(5)* exp(-z(1)*10^12*(t-z(2)*10^-6).^2).*cos(2*pi*z(3)*10^6*(t-z(2)*10^-6)+z(4));
ff =sum((y-sigme).^2)/NN;
end
it is used in an optimization toolbox which after optimizing it the output is a matrix z which I have in my workspace.
My second function is like this:
function ff = secstep(zz)
sigme1=zz(1)* exp(-z(1)*10^12*(t-zz(2)*10^-6).^2).*cos(2*pi*z(3)*10^6*(t-zz(2)*10^-6)+z(4));
end
I have also been attempting to optimize this function, but this time matlab can't call (what I meant by reading in my first post) matrix z.
The error is this:
Undefined function 'z' for input arguments of type 'double'.
After checking your code I should say that, you need to pass the variables to your function or make the variables global. check out the following link:
How declare a global variable in MATLAB
example:
function tic
% TIC Start a stopwatch timer.
% TIC; any stuff; TOC
% prints the time required.
% See also: TOC, CLOCK.
global TICTOC
TICTOC = clock;
function t = toc
% TOC Read the stopwatch timer.
% TOC prints the elapsed time since TIC was used.
% t = TOC; saves elapsed time in t, does not print.
% See also: TIC, ETIME.
global TICTOC
if nargout < 1
elapsed_time = etime(clock, TICTOC)
else
t = etime(clock, TICTOC);
end
I suggest you declare the z as a global variable in each function and in the main file simply:
global z
The optimization algorithm that I was using was GA(Genetic Algorithm). I wrote this function in order to get what I wanted:
function [zz,fvalzz] = secstep(z,NVARS)
[zz,fvalzz]=ga(#secfun,NVARS);
function ff = secfun(zz)
y=.5*exp(-35*10^12*(t-2e-6).^2).*cos(2*pi*5e6*(t-2e-6)+0);
sigme2=zz(1)* exp(-z(1)*10^12*(t-zz(2)*10^-6).^2).*cos(2*pi*z(3)*10^6*(t-zz(2)*10^-6)+z(4)); %z vector has been calculated from another function.
NN=length(y);
ff =sum((y-sigme2).^2)/NN;
end
end
I have a program running a loop I want to have two time counters, one for the loop, that will tell me how log did one iteration of the loop took, and one for the entire program. To the best of my knowledge tic and toc will work only once.
You're only familiar with this tic toc syntax:
tic; someCode; elapsed = toc;
But there is another syntax:
start = tic; someCode; elapsed = toc(start);
The second syntax makes the same time measurement, but allows you the option of running more than one stopwatch timer concurrently. You assign the output of tic to a variable tStart and then use that same variable when calling toc. MATLAB measures the time elapsed between the tic and its related toc command and displays the time elapsed in seconds. This syntax enables you to time multiple concurrent operations, including the timing of nested operations (matlab documentation of tic toc).
Here's how to use it in your case. Let's say that this is your code:
for i = 1:M
someCode;
end
Insert the tic and toc like this:
startLoop = tic;
for i = 1:N
startIteration = tic;
someCode;
endIteration = toc(startIteration);
end
endLoop = toc(startLoop);
You can also use the above syntax to create a vector for which the ith element is the time measurement for the ith iteration. Like this:
startLoop = tic;
for i = 1:N
startIteration(i) = tic;
someCode;
endIteration(i) = toc(startIteration(i));
end
endLoop = toc(startLoop);
You can use tic and toc to time nested operations, from the Matlab help for tic:
tStart=tic; any_statements; toc(tStart); makes the same time measurement, but allows you the option of running more than one stopwatch timer concurrently. You assign the output of tic to a variable tStart and then use that same variable when calling toc. MATLAB measures the time elapsed between the tic and its related toc command and displays the time elapsed in seconds. This syntax enables you to time multiple concurrent operations, including the timing of nested operations
I'm not able to try this right now, but you should be able to use multiple tic and toc statements if you store the tic values into variables.
Read Matlab's documentation on this, there is even a section on nesting them. Here is a rough example:
tStartOverall = tic;
...
tStartLoop = tic;
<your loop code here>
tEndLoop = toc(tStartLoop);
...
tEndOverall = toc(tStartOverall);