简体   繁体   中英

I would like to create a loop that creates a different variable name after each iteration

I would like to create a set of distinct variables in the form of e_1 , e_2 , e_3 ... The number of variables would depend on the value of k as shown below.

for i = 1:k

  "create variable" = e_"i";

end

I will then want to call these variables; hence, I would need another loop that would be able to assign the correct value to each variable created before, or pull something out of the variable.

One option would be to do something like this:

kk = 10;
for ii=1:kk
  eval(['e_' num2str(ii) '=[];'])
end

I got just the fix for you. Was just trying to do the same thing. Needed to be able to create an array with an infinate number of variables. Here's what I came up with. Well, I was adding 2 variables at a time, so it may be a little different than your version.

String[] parts;
String x = "";
String var = "";
int i = 0;
//to add variables
if(x.contains("-"){
 x+="-"+var;
}
else{
x+=""+var;
}

then to get them, u just use.

if(x.contains("-")){
parts = x.split("-");
while(i<parts.length){
  var=parts[i];
  i++;
 }
}
else if (x!=""){
var = x;
}

Changing the variables is a whole other story. I'll let your gifted mind figure that out. I don't need that part yet.

*hint, you're going to have to loop and put together the whole String back together with the new variables. Hope this helped. Felt like this was a new way of looking at it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM