Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Strcat to create dynamic variable names

Tags:

eval

matlab

I have a process which is repeated on a set of data stored in separate folders. Each time a certain folders data is processed I need new variable names as I need to results separate after the initial processing is finished for more processing. For example at the start of each new block of the repeated function I declare sets of arrays

Set_1 = zeros(dim, number);

vectors_1 = zeros(dim, number);

For the next set of data I need:

`Set_2 = .........`

and so on. There is going to be alot of these sets so I need a way to automate the creation of these variables, and the use the new variables names in the function whilst maintaining that they are separate once all the functions are completed.

I first tried using strcat('Set_1',int2str(number)) = zeros(dim, number) but this does not work, I believe because it means I would be trying to set an array as a string. I'm sure there must be a way to create one function and have the variables dynamically created but it seems to be beyond me, so it's probably quite obvious, so if anyone can tell me a way that would be great.

like image 239
James Burnstone Avatar asked Jul 27 '26 04:07

James Burnstone


1 Answers

I'd not do it like this. It's a bad habit, it's better to use a cell array or a struct to keep multiple sets. There is a small overhead (size-wise) per field, but it'll be a lot easier to maintain later on.

If you really, really want to do that use eval on the string you composed.

like image 88
jpjacobs Avatar answered Jul 30 '26 00:07

jpjacobs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!