re-running a for loop for 100s of different ranges

Hi experts hope you can help me :)
I would like to re-run a 'for' loop for 100s of different ranges. the loop is below in the code. I believe it will need a loop in a loop but I don't know how to do this, I have a list of the ranges (x and y values that make the range), but how do I implement this so that it does the loop for k=58:69 then again for K=78;89 then for k=90:102 ect, (no pattern in rages). Hope this is clear what am trying to do. any input would be much appreciated.
%Clearing and formatting
clear all;
close all;
format long;
%getting range of each scan
Scan_ranges = xlsread('aCopy of spray 144_plume scans','B:C');
%creating results file
z=[];
% control loop to process all files in the range k
for k = x:y
%Importing data
open_file = csvread((sprintf('C1charlie %d.csv', k)),5,0);
%Seperating data
Time = open_file(:, 1);
Ampl=open_file(:,2);
%Seperating positive and negative data for time and amplitude
ind_pos = find(Time>0);
ind_neg = find(Time<0);
Ampl_pos= Ampl(ind_pos);
Ampl_neg= Ampl(ind_neg);
%Finding the standared deviation of the amplitudes and muiliplying by 1e4
SD_I_pos = std(Ampl_pos)*1e4;
SD_I_neg = std(Ampl_neg)*1e4;
%Choosing the smaller of the two amplitudes
if SD_I_pos>SD_I_neg
z = [z mean(Ampl_neg)*1e4];
else
z = [z mean(Ampl_pos)*1e4];
end
end

 Accepted Answer

Cedric
Cedric on 13 Jul 2013
Edited: Cedric on 13 Jul 2013
You should work on a much simpler code to understand how it works. Here is an example..
ranges = [3, 8; 2, 5; 1,7] ; % 3 ranges from your Excel file.
for rId = 1 : size(ranges, 1) % Outer loop.
for x = ranges(rId,1) : ranges(rId,2) % Inner loop.
fprintf('%d,', x) ;
end
fprintf('\n') ;
end
This code outputs:
3,4,5,6,7,8,
2,3,4,5,
1,2,3,4,5,6,7,

4 Comments

First of all thanks so much for your reply, I was able to apply your example to my code easily.
But I would like to output the data into the workspace so I may work with and plot each range output, I tried using sprintf, but that only gave me a string of data, unlike how its outputs above.
Could you show me how I could do this on your example?
thanks
alex
The following example shows how to store some results in a cell array. Keep in mind that the "best" way to implement computations in MATLAB is often by vectorizing loops though.
What we do here is, for each range, to take all values of x, and add random components to them.
If all ranges had the same number of elements, we could store results in a 2D numeric array with one row per range. As ranges don't have the same number of elements, we must save each result in its own 1D array. Here we save each one of these 1D arrays in a cell array (an array of cells), because they can store inhomogeneous content. More explicitly, the cell arrays is named results and you'll notice that we address it using curly brackets, e.g. results{1}, which means "content of cell number 1 of cell array results". Ability to store inhomogeneous content means that results{1} could store a 1D numeric array, results{2} a 3D array, results{3} an array of function handles, results{4} a Java hashtable, etc.
ranges = [3, 8; 2, 5; 1,7] ; % 3 ranges from your Excel file.
results = cell(size(ranges,1), 1) ; % Build empty cell array for
% storing one result per range.
for rId = 1 : size(ranges, 1) % Outer loop: ranges
xValues = ranges(rId,1) : ranges(rId,2) ;
result = zeros(size(xValues)) ; % Temporary variable for
% building current result.
for xId = 1 : length(xValues) % Inner loop: x values
result(xId) = xValues(xId) + rand(1) ;
end
results{rId} = result ; % Store result in cell array.
end
% At this point data extraction/computation is done, and you have ranges
% and results in the workspace that you can use, e.g. plotting results.
figure(1) ; hold on ; grid on ;
for rId = 1 : length(results) % Loop over results and
plot(results{rId}) ; % plot them.
end
Hope it helps!
Cedric
I have managed to implement this how I need to.
Thanks Cedric, you helped me a lot!

Sign in to comment.

More Answers (1)

I have added variables for the loop, but it only does the loop for the first set of variables, how can i change it so the loop runs for all the ranges? thanks
%getting range of each scan Scan_rangesx = xlsread('aCopy of spray 144_plume scans','B:B'); Scan_rangesy = xlsread('aCopy of spray 144_plume scans','C:C');
x = (Scan_rangesx+100000); y = (Scan_rangesy+100000);
%creating results file z=[];
% control loop to process all files in the range k for k = x:y
%Importing data open_file = csvread((sprintf('C1charlie %d.csv', k)),5,0);
%Seperating data Time = open_file(:, 1); Ampl=open_file(:,2);
%Seperating positive and negative data for time and amplitude ind_pos = find(Time>0); ind_neg = find(Time<0); Ampl_pos= Ampl(ind_pos); Ampl_neg= Ampl(ind_neg);
%Finding the standared deviation of the amplitudes and muiliplying by 1e4 SD_I_pos = std(Ampl_pos)*1e4; SD_I_neg = std(Ampl_neg)*1e4;
%Choosing the smaller of the two amplitudes if SD_I_pos>SD_I_neg z = [z mean(Ampl_neg)*1e4]; else z = [z mean(Ampl_pos)*1e4]; end
end
%plotting plot(z)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 12 Jul 2013

Community Treasure Hunt

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

Start Hunting!