MATLAB Answers

Joe Gee

changing input variable loop

Asked by Joe Gee
on 10 Apr 2019
Latest activity Edited by Joe Gee
on 10 Apr 2019
Hi All,
I am very new to matlab.
I have an issue where i need to change range of an input variable on each loop. I have attached the input data above.
There are multiple ranges within this input variable that i need to calculate through the same loop.
Range1 = Input variable(9203:9805)
Range2 = Input variable(93596:104564)
I have a lot of these ranges to run through.
Is there a way of running a loop function and changing the range each time, and outputting that data as A1,A2,A3 etc?
I have read elsewhere that dynamic variables and bad, and ive been trying to get round this issue, but i cant seem to work it out.
Thank you


There are many ways to do what you want. None of them will involve creating numbered variables. The output would be in a single cell array of similar.
Most likely, you don't even need loops. However, we need details of what it is you want to calculate.
Dynamic variable names are one way that beginners force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know why:
Dynamic variable names are also very easy to avoid: just use simple and efficient indexing, or a table, or a structure.
How are these ranges defined / generated ?
on 10 Apr 2019
@Joe Gee: Did you read the posted link already?
Use Range{1}, Range{2}, ... instead. Then loops are easy.

Sign in to comment.

1 Answer

Answer by Guillaume
on 10 Apr 2019

I won't belittle the point about numbered variables. Read Stephen and Jan links. Suffice to say, if you start numbering or naming your variables in any serial manner, you need to stop and rethink.
As said, it's most likely that you don't need a loop. You certainly don't need one for something as simple as averaging even if it's averaging different ranges. But without seeing your code, we can't guess what you're doing exactly.
If your processing code is a script, then first thing you need to do is convert it into a function that accepts either a range vector (eg. the vector 9203:9805 for your first range) or the start and end of the range (eg. 9203 and 9805 for your first range. Once that is done, calling your function for each range is trivial.
ranges = [9203 9805
935596 104564
... more rows
result = cell(size(range, 1), 1); %assuming the function returns variable size output. If scalar then
%result = zeros(size(range, 1), 1);
for row = 1:size(ranges, 1)
result{row} = yourfunction(ranges(row, 1), ranges(row, 2)); %if taking range start and end
result{row} = yourfunction(ranges(row, 1):ranges(row, 2)); %if taking a range vector
The loop can also be replace by an arrayfun:
ranges = [9203 9805
935596 104564
... more rows
result = arrayfun(@yourfunction, ranges(:, 1), ranges(:, 2), 'UniformOutput', false);
But again, it's most likely that none of this needed and you could just use plain indexing if you're just averaging.

  1 Comment

S1= Input variable(93737:95703,:);
T1= TimeSeries(93737:95703,:);
TC1 = TimeSeries(94720,:);
%above 3 lines are the ranges for one output
%T1 and TC1 are just corresponding seconds to input variables S1
%Centre of time series
middleIndex = find(T1 < TC1, 1, 'last');
% Define threshold.
thresholdValue = 300;
% Find left index
leftIndex = middleIndex;
for k = middleIndex : -1 : 1;
if S1(k) > thresholdValue;
leftIndex = k;
TA1L = T1(k);
% Find right index
thresholdValue = 300;
rightIndex = middleIndex;
for k = middleIndex : length(T1);
if S1(k) > thresholdValue;
rightIndex = k;
TA1R = T1(k);
Rateoffuelconsumption1 = (0.5*1000) * (TC1 - TA1L);
Rateoffuelconsumption2 = (0.5*1000) * abs(TC1 - TA1R);
Y1 = (Rateoffuelconsumption1-Rateoffuelconsumption2)/2
This is the code it will run though, i should apologies and say it isnt an average, however they are just simple calulation to ultimatly fid Y1 which is total fuel consumption per unit time.
I need to input each range (300 seperate ranges) and output a single value, this can be in a single column matrix.
Very sorry if above code is sloppy and not accurate, but for individual case it seem to work. I'm a university student and find matlab extremely complex.

Sign in to comment.