# how to increase the number in for loop as i explained below??

1 view (last 30 days)
M.Prasanna kumar on 20 Aug 2019
Answered: Bhargavi Maganuru on 23 Aug 2019
for tt = 1:24
for rr = 1:24
for mm = 1:10
for nn = 1:50
[~,idx] = min(abs(time(:,1) - tof(mm,nn,rr,tt)));
voltage(mm,nn,rr,tt) =t(tt)[(idx,rr+1)]; %% here is the error
end
end
end
end
EVERY TIME WHEN tt VALUE INCREASES t(tt) should increase [t is a matrix]
if i write t1(idx,rr+1) it is working
i want to change from t1 to t2, then t3, then t4, and so on up to t24

Stephen Cobeldick on 20 Aug 2019
"i want to change from t1 to t2, then t3, then t4, and so on up to t24"
Do NOT do this!
Dynamically accessing variables names is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know why:
Instead of putting pseudo-indices into variable names, why not just use actual indexing? Indexing is simple, neat, easy to debug, and very efficient (unlike what you are trying to do).
Putting numbers into variable names is a sign that you are doing something wrong.
M.Prasanna kumar on 20 Aug 2019
sir what is meant by actual indexing?? could you please explain
Stephen Cobeldick on 20 Aug 2019
"sir what is meant by actual indexing??"
Possibly a cell array or an ND array would suit your task.

Bhargavi Maganuru on 23 Aug 2019
You can create multidimensional array t to store t1, t2, ..t24 using “cat” function t=cat(3,t1,t2,..t24);
Now you can access t1 using t(:,:,1) and t2 using t(:,:,2) and so on.
You can modify line in your code as
voltage(mm,nn,rr,tt) =t(idx,rr+1,tt);
Hope this helps.