1

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 this array into separate variables for individual processing elsewhere, I can't call my data like this BigData(:,:,n).

So I would like to create a loop where I create new variables like so

for i=1:n

createVariable('user_' i) = BigData(:,:,i);

end

How do I do this without writing n new variables by hand every time?

user_1 = BigData(:,:,1);
user_2 = BigData(:,:,2);
user_3 = BigData(:,:,3);
.
.
.
3
  • 2
    I strongly discourage this approach. However, in the mathworks forum you might find what you are looking for. Commented Jul 15, 2014 at 13:48
  • Down-voting doesn't change the case that the Neural Network toolbox only accepts two dimensional arrays. Commented Jul 15, 2014 at 13:51
  • 1
    While that may be the case, you could still you cells or structs to come up with 2D-arrays. Commented Jul 15, 2014 at 13:53

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I still fail to see why he can't supply the layers of BigData(:,:,ii) to the NN function inside the loop, or wrap it into a cell array. Using it somewhere else, will still require hardcoding, user_1,..., etc.
The GUI doesn't accept it and I've only been using matlab for less than a week so I haven't learnt how to call the NN function yet in the script. Also another reason is I need to provide a progressive and transparent series of user friendly variables that my Professors can understand what they mean without reading every line of my code. Like I said, I'm not doing this to simply go against good coding practices, this is just an extra on the side to save my progress with the data so far. I will go back to the Multi dimensional array in my own code. As like you I appreciate good coding practices.
Could you clarify exactly which GUI requires named 2-D arrays? (You mention neural networks - but I don't know enough about that to kno
The NN toolbox popup, only recognizes 2-D variables in your workspace. Anything in 3 or more dimensions is not a valid input.
1

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

4 Comments

Stop spreading bad programming practices and read the description of my profile. Also, according to your logic, I could say, guns are bad, but ehre you go kid, they get things done (drama intended).
I add value by downvoting this practice so that novices won't waste their time on debugging nightmare code once their project is deep hardcoded with uncomprehensible eval statements. Also, I do raise the question that the OP disclaimer is absolutely not valid, unless he elaborates.
Well, is has been said on and on in this thread that it is no good to use code like this. So everybody who reads this will hardly overlook it. On the other hand it might actually help somebody learning something. Voting down does not help anybody here. It would add value if you would come up with a solution that makes sense instead.
I've tested this code and it works. Thanks @Eli Duenisch. Even though the description of the problem says I need to do this without wanting to I don't see why people are being so militant?? I know it's not the best way but there is no other way, when it needs to be done. Also as explained better in a comment on the accepted answer, it also serves to show to my professors that my data has/can be segmented. Thanks for your help! An uninterested viewer of my work should not need to be a Matlab expert to read the results. Though all this explanation shouldn't have really been needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.