0

I have a code in which I create a large number of the same variable and I would like to store all of its values. Normally I would use for loops for this and store all of them, but due to some complications it is not possible at the moment.

I have to calculate the variable x, store it as x1. Calcute the x with different conditions and store it as x2 etc. The problem I have that I would have to manually change all the x's in my code to x2, x3, x4 etc. And there are a lot of them. Is there a way to it more efficiently?

To better illustrate:

x1 = 5*y(1);

%some calculations with y

x2 = 5*y(2);

% some claculations with y

x3=5*y(3);
5
  • 2
    Could you illustrate with something closer to what you are actually using? Because I am guessing it won't be as straight-forward as just scaling by 5 or is it? My pointer for you is still hovering around at suggesting vectorized methods. Commented May 19, 2014 at 13:52
  • 2
    so why can't you store it as x(1) = 5*y(1) so on and so forth? Commented May 19, 2014 at 13:53
  • Yes I could, but I would have modify all the x's in my code to x(1) and x(2) etc. It would take a lot of time Commented May 19, 2014 at 13:57
  • Just use "search and replace" function in any text editor. It should not be that hard. x(1) and x(2) are the right way to go. Commented May 19, 2014 at 14:10
  • @Andrey my answer below indicates how to do that programmatically inside of MATLAB using regular expressions. Then again, most text editors will have similar functionality. Commented May 19, 2014 at 14:30

1 Answer 1

2

If you are really desperate you could use eval and loop through all of your variable names, assuming they are numbered 1 through n.

count = 1;
while exist(['x',num2str(count)],'var')
    eval(['x(count)=x',num2str(count),';'])
    count = count + 1;
end

This code assumes everything is numbered x1, x2, x3, etc. It will continue looping until the next "sequential" x no longer exists. This would go at the end of whatever script or function you are running after all the declarations have happened.

Just note that storing everything the way you did initially is very frowned upon. It makes code hard to read and cumbersome, and it not very dynamic.

Either way, the code above should do the trick.

You could also write a regular expression to replace all xn and whatever instances to x(n).

Just run the following lines of code, giving it the file name you want to convert, and boom it will handle all of the replacement for you

fileName = 'findReplace.m';
replaced = regexprep(fileread(fileName),'x([0-9]+)','x($1)');
fh = fopen(fileName,'w');
    fprintf(fh,replaced);
fclose(fh);
Sign up to request clarification or add additional context in comments.

4 Comments

For completeness, maybe you should add a vectorised version of the calculations as well?
@kkuilla I do not know if there is a simple way to vectorize this.
I misread the question. Apologies. The question was how to change the variable names. My brain automatically thought that the question was about how to store x1,x2` more efficiently e.g x(1), x(2). +1 though...
@kkuilla Thank you. I really think the easiest solution would be to replace all instances of x1 with x(1) etc. The second portion does this. I mean both solutions are valid, just the first one, having to use eval, will run kinda slowly and is frowned upon, especially with large numbers of variables

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.