MATLAB Answers

Megha
0

how to set new variable for each cycle?

Asked by Megha
on 13 Aug 2018
Latest activity Commented on by Megha
on 16 Aug 2018
Accepted Answer by KSSV

The figure shows the data of the form sine wave. Now, I wish to define one new variable (e.g. a1, a2, a3) each corresponding to one cycle? Is it possible?

  2 Comments

"Now, I wish to define one new variable (e.g. a1, a2, a3) each corresponding to one cycle?"

The MATLAB documentation specifically advises against doing this: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array"

source: http://www.mathworks.com/help/matlab/matlab_prog/string-evaluation.html

"Is it possible?"

Sure, it is possible, but only if you want to force yourself into writing slow, complex, buggy code. Read this to know more:

http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval

It is much simpler and more efficient to use indexing. You should use indexing.

Sign in to comment.

1 Answer

Answer by KSSV
on 13 Aug 2018
 Accepted Answer

Get the indices of the least values...and you can divide/ name your data. Read about min, findpeaks and logical indexing.

  2 Comments

@KSSV and @Walter, sorry, I could not exactly understand your suggestions. However, I found some other way to solve this problem.

Thanks for the help

Sign in to comment.