A way to dynamically create variables in Matlab?

后端 未结 2 1576
[愿得一人]
[愿得一人] 2021-01-16 07:38

The case I am working on is dividing a big three-dimensional array of data that I have collected using good coding practises (etc...) and now I need to segment the layers of

相关标签:
2条回答
  • 2021-01-16 07:45

    Your disclaimer sounds convincing :-) I'll get those downvotes too. But, to be clear: using separate variables for this is bad practice.

    You can use assignin to create the variables and assign them values:

    for ii = 1:n
        assignin('base', ['user_' num2str(ii)], BigData(:,:,ii));
    end
    

    And yes, using separate variables for this is bad practice.

    0 讨论(0)
  • 2021-01-16 08:02

    Try using eval() - but as you said it is seen as a very bad practice. If you still want to use this, it is straight forward like:

    for i = 1:n
        eval(['user_',num2str(i),' = BigData(:,:,',num2str(i),');']);
    end
    
    0 讨论(0)
提交回复
热议问题