使用Strcat创建动态变量名称 [英] Using Strcat to create dynamic variable names

查看:62
本文介绍了使用Strcat创建动态变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对存储在单独文件夹中的一组数据重复进行此过程.每次处理某些文件夹数据时,我都需要新的变量名,因为在初始处理完成后,我需要将结果分开以进行更多处理. 例如,在重复功能的每个新块的开头,我声明了数组的集合

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.

我首先尝试使用strcat('Set_1',int2str(number)) = zeros(dim, number),但这不起作用,我相信这是因为这意味着我将尝试将数组设置为字符串.我确定必须有一种方法来创建一个函数并动态创建变量,但是它似乎超出了我的范围,所以这很明显,所以如果有人可以告诉我一种很棒的方法.

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.

推荐答案

我不会这样做.这是一个坏习惯,最好使用单元格数组或结构保留多个集合.每个字段的开销很小(按大小计),但是以后维护起来会容易得多.

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.

如果您确实想要这样做,请对所编写的字符串使用eval.

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

这篇关于使用Strcat创建动态变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆