0

I want to define 4 objects with their names 'A','B','C' and 'D' in a for loop

I tried something like this but it didn't work

    list = {'A','B','C','D'}
    for i = 1:1:length(list)
        list{i} = class
    end
  • You want to create variables `A`, `B`, `C` and `D`? Yeah, don't try to do that in a loop. There's nothing wrong with creating those variables, but variable names are not data, don't try to mix those, it leads to headaches and sorrow. – Cris Luengo Nov 17 '22 at 22:00
  • 1
    If you explain why you want to do this (your larger problem), we might be able to help you. – Cris Luengo Nov 17 '22 at 22:01
  • Do **NOT** create those variables. This is called *Dynamic variable naming* and is considered [bad, very bad. Very very bad](https://stackoverflow.com/a/32467170/5211833). You just shoot yourself in the foot, because after you have created these variables, you'll want to process them automatically, leading you into a rabbit hole that'll lead to obscure error, slow down your code execution and cause you a whole lot of pain in general. Just don't do that. Simply use your cell, a numeric array, a struct, or whatever datatype is appropriate for your case. – Adriaan Nov 18 '22 at 07:27

0 Answers0