for loop in matlab

Thread Starter

moneyk

Joined Feb 26, 2008
9
Hello matlab experts,

I have implement a for loop that executed certain commands only after particular time intervals for a time range. For example, read voltage every 30 seconds for 30 minutes. How do I implement that in a for loop.

Thank you.
 

Dave

Joined Nov 17, 2003
6,969
Personally I would use a while-loop with some sort of flag (the flag representing the status of your read), i.e. while there is data to read, read it.

To manage the time sampling, you could look at constructing a timer object using the timer function, and then passing that object to the wait function to stage the time sample. All you have to do then is embed your voltage read code within the while-loop padded by the timer at the end of the loop.

Dave
 

Thread Starter

moneyk

Joined Feb 26, 2008
9
Hi,

thanks for your quick reply. I am actually a beginner in programming and matlab so I am not aware of the timer function. Below is what I am doing:

output_voltage = Measure_voltage (gdm)
dlmwrite(output_file, [time_stamp output_voltage], 'delimiter', '\t', 'newline','pc','-append');

So I am measuring the voltage and writing it in a text file. I want to do this every 10 seconds for say 30 minutes. How do I use the timer function. I looked up timer function in the help menu but am not able to figure it out. The data is always present at the output of the meter. Matlab makes the measurement everytime I call the function Measure_voltage.

Thank you.
 

Dave

Joined Nov 17, 2003
6,969
Hi,

thanks for your quick reply. I am actually a beginner in programming and matlab so I am not aware of the timer function. Below is what I am doing:

output_voltage = Measure_voltage (gdm)
dlmwrite(output_file, [time_stamp output_voltage], 'delimiter', '\t', 'newline','pc','-append');

So I am measuring the voltage and writing it in a text file. I want to do this every 10 seconds for say 30 minutes. How do I use the timer function. I looked up timer function in the help menu but am not able to figure it out. The data is always present at the output of the meter. Matlab makes the measurement everytime I call the function Measure_voltage.

Thank you.
Ok, it seems Mathworks have made this quite a bit easier than it used to be.

What I would do is embed your Measure_voltage and dlmwrite functions into a single function - this function encapsulates everything to do with taking a single voltage reading. For the purposes of the following explanation lets assume they are encapsulated into a function called voltage_read (you will see why we do this later).

Now I suggest you read up on objects and how to pass and modify attributes within objects, but to get you started I will let you see how to manipulate some of the data. What you need to do is create a timer object:

Rich (BB code):
v_timer = timer;
Typing v_timer at the command prompt without a trailing ";" will list the properties of the timer.

The properties that are of interest to you are ExecutionMode, Period, and TimerFcn. Go to the Matlab help to read up on what these attributes are.

Now you need to configure them:

1) ExecutionMode - you want to be FixedRate because you are sampling at a repetative fixed time rate, so:

Rich (BB code):
set(v_timer,'ExecutionMode','FixedRate')
2) Period - you want to be 30 for 30 seconds, so:

Rich (BB code):
set(v_timer,'Period',30)
3) TimerFcn - this is what the timer calls on each iteration which, beacsue you encapsulated the voltage measurement process into the voltage_read function, is voltage_read, so:

Rich (BB code):
set(v_timer,'TimerFcn','voltage_read')
There are many more configuration options, but that should hopefully give you start and will allow you to take voltage readings every 30 seconds.

To start the timer type:

Rich (BB code):
start(v_timer)
And to stop it type:

Rich (BB code):
stop(v_timer)
My suggestion is that poll the voltage input to see that there is still a value and stop the timer as necessary.

Check my syntax, I do not guarantee I have spelt everything correctly.

Dave
 

Thread Starter

moneyk

Joined Feb 26, 2008
9
Hi Dave,

I did as you suggested. I embedded Measure_voltage and dlmwrite into a single function. I named it as Measure_voltage only. The function os below

function volt = Measure_voltage (gdm)
fprintf (gdm, '*RST');
fprintf (gdm, 'conf:volt:dc 10,1e-3');
fprintf (gdm, 'inp:imp:auto ON');
fprintf (gdm, 'init');
volt = str2num(query(gdm, 'fetch?'));
output_file = 'C:\Test\output_data.txt'
time_stamp=clock;
dlmwrite(output_file, [time_stamp volt], 'delimiter', '\t', 'newline','pc','-append')

I am able to run the function and get the desired output.

Based on your inputs and the help menu, I created the following timer object:
t = timer('ExecutionMode', 'fixedRate', 'TimerFcn',@Measure_voltage, 'Period', 10.0)

When i execute the above command I get this:
imer Settings
ExecutionMode: fixedRate
Period: 10
BusyMode: drop
Running: off

Callbacks
TimerFcn: @Measure_voltage
ErrorFcn: ''
StartFcn: ''
StopFcn: ''

Which is correct. Now when I use this in my main program which is:

function output_with_timer ()
gdm = setup_gpib()
t = timer('ExecutionMode', 'fixedRate', 'TimerFcn',@Measure_voltage, 'Period', 10.0)
start(t)

I get the following error:
??? Error while evaluating TimerFcn for timer 'timer-43'

Too many input arguments.

Can you suggest me why I am getting this error?
Thank you very much.
 

Thread Starter

moneyk

Joined Feb 26, 2008
9
Hello Dave,

I was able to run the program. My main program looks like this:

function output_with_timer ()
gdm = setup_gpib()
t = timer('ExecutionMode', 'fixedRate', 'Period', 10.0)
t.TimerFcn = {@Measure_voltage, gdm};
start(t)

This program captures voltage measurement and writes it to a text file every 10 seconds. This goes on forever. How do I use the stop timer command if I want to stop it after 20 minutes. So I want it to capture data every 10 seconds for 20 minutes and then stop. I was thinking of using a while loop. But I am not sure how it would work. Can you help me in this? How can I make a condition for it stop after 20 minutes or rather any time I want?

Thank you very much.
 

Dave

Joined Nov 17, 2003
6,969
Hello Dave,

I was able to run the program. My main program looks like this:

function output_with_timer ()
gdm = setup_gpib()
t = timer('ExecutionMode', 'fixedRate', 'Period', 10.0)
t.TimerFcn = {@Measure_voltage, gdm};
start(t)

This program captures voltage measurement and writes it to a text file every 10 seconds. This goes on forever. How do I use the stop timer command if I want to stop it after 20 minutes. So I want it to capture data every 10 seconds for 20 minutes and then stop. I was thinking of using a while loop. But I am not sure how it would work. Can you help me in this? How can I make a condition for it stop after 20 minutes or rather any time I want?

Thank you very much.
Sorry I missed this yesterday.

Given you know how many iterations of Measure_voltage you have. That is for 1 sample every 10 seconds for 20 minutes, you can set the TasksExecuted attribute to 6*20 = 120, which will execute the Measure_voltage function 120 giving one sample every 10 seconds for 20 minutes.

This is obviously limited by the fact you are explicitly stating how many iterations - the measurement period and time is hard-coded into the function. If you wanted it to be dynamic, you would need to poll the voltage input and have Measure_voltage check and modify TasksExecuted dependant on whether data is available.

Dave
 
Hi there i have a question about for loops in matlab, i have a matlab exam very soon and am working through some questions and i don't understand how to reach the value for ires at the end of the loop. Hope someone can help.

Question:
Examine the following loops (without matlab) and determine the value in 'ires' at the end of each of the loops.

question1.

ires=0;
for index = 1:10
ires = ires+1;
end

question 2.

ires=0;
for index = 1:10
ires=ires+index;
end

question 3.

ires=0;
for index1=1:10
for index2=index1:10
if index2>6
break;
end
ires=ires+1;
end
end

Thankyou
 
Top