Storing values of a variable in a new one

Hello! I have a variable with two rows. One contains time points and the other numbers that indicate which event happened at these time points. How do I best go about going through this variable and storing the values of the different events in new variables. Grateful for any help! Cheers!

5 Comments

Thorsten
Thorsten on 22 Sep 2015
Edited: Thorsten on 22 Sep 2015
Usually it is best NOT to store nicely aggregated values in new separate variables. So what do you want to do with the individual variables that you cannot do with the single variable of all data?
I basically want to create plots with subsets of the data (and combinations) based on the event codes.
Thorsten
Thorsten on 22 Sep 2015
Edited: Thorsten on 22 Sep 2015
Then I would suggest to compute the proper indices into your main variable; that's probably a lot easier and clearer than splitting into multiple variables and working on those.
Thank you for your answer.Might be a stupid question (don't have much experience with matlab), but how would I do that?
Thorsten
Thorsten on 22 Sep 2015
Edited: Thorsten on 22 Sep 2015
To answer that it would be best to post the variable and precisely describe what you want to plot; in fact, it would be best to pose a new question, because it does not conform to your original question and its title any more.

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 22 Sep 2015
Edited: Stephen23 on 22 Sep 2015
"How do I best go about going through this variable and storing the values of the different events in new variables"
Don't.
Keep your data in one numeric array, and then use indices to access the parts of it that you need to do. Trying to create variable names dynamically is a poor programming practice and will be obfuscated, hard to debug, and slow to run.
Learn to use MATLAB efficiently:
  • Keep your data in numeric arrays.
  • Do not use multiple arrays when one will do.
  • Use the dimensions of those arrays, rather than splitting them up into multiple variables.
  • Only use cell arrays when your data does not fit neatly into one array due to mismatching dimensions.
  • Do not create variables dynamically.
  • Learn to write vectorized code.
Why it is a bad practice to create variable names dynamically:

More Answers (0)

Tags

Asked:

on 22 Sep 2015

Edited:

on 22 Sep 2015

Community Treasure Hunt

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

Start Hunting!