How to reverse a series of variable assignments?

In my code, I frequently have blocks of code of the form
var1 = setting1;
othervar2 = othersetting2;
somevar3 = somesetting3;
where I don't have names of the variables or settings in some way that i can program. Even if i did, I think i would be using eval, and i don't want to do that.
It's common that i later wish to perform analogus assignments in reverse, so i would have a block like
setting1 = var1;
othersetting2 = othervar2;
somesetting3 = somevar3;
This can be very tedious to write out. Does there exist a standard way to performing a line-by-line edit which swaps the left and right side of the variable assignment?
I imagine If i copy the code into a text file, it wouldn't be too hard to write a script that produces a new text file with all the variable assignments swapped, but I wonder if this exists already or if there's a term for this.

 Accepted Answer

I did find a quick solution. On the File Exchange Alessandro Masullo has a function which aligns a block of variable assignments by their equal signs. https://www.mathworks.com/matlabcentral/fileexchange/47918-align-equal-sign
I modified the code to simply place the pre-equal sign part where the post-equal sign part was and vice-versa. my modification is explained in the comments on his alignEquals upload. then running the unmodified alignEquals makes things looks good again.

More Answers (1)

Rik
Rik on 11 Mar 2021
Edited: Rik on 11 Mar 2021
I tend to use deal to make it more or less compact:
[var,othervar,somevar]=deal(setting1,othersetting,somesetting);
Now if I want to reverse this I only need to swap the parts between the braces and parentheses, which is easy and fast.
However, you could do some magic with eval (and inputname to make it somewhat robust). The result will not be very clean. You might also consider something like the function below.
function varargout=flipflop_assign(order,varargin)
varargout=varargin(order);
end

6 Comments

I tend to benefit from clarity in my code over compactness, hence i like to hide the code in fake for loops that i can fold. My brain can read and understand the vertical orientation much more easily.
I don't really understand how your question, comment and answer relate to each other, but from your vote it seems at least my answer help you, so, glad to be of service.
Your suggestion to use deal is a good one. And indeed just copy-pasting works very well when the assignments are written that way.
However, I find it very difficult to easily read such code. I prefer to just take as much space as i want with a long vertical list of individual assignments. This is simply a quality of life perfernce of mine.
So, your suggestion will work perfectly, and in cases where compactness is important, I would certainly use deal, but that requires me to format my code in a way that I normally find quite difficult to work with.
my solution allows me to preserve the verbose, space-hogging formatting which I prefer, without having to manually type out a long column of variable assignments.
as an example, i'm making an app to fit my data and i have a lot of variable assignments as follows:
for folding_variable_assignments = 1:1 % when i dont need to see this, i can fold it up
peak_master_handling(app);
mag1 = app.Magnitude1EditField.Value;
rx1 = app.xsize1EditField.Value;
ry1 = app.ysize1EditField.Value;
x01 = app.x01EditField.Value;
y01 = app.y01EditField.Value;
ang1 = app.Angle1EditField.Value;
sep1 = app.DiffractedSeparation1EditField.Value;
besarg1 = app.DiffractedBessel1EditField.Value;
slp1 = app.DiffractedSlope1EditField.Value;
params1guess = [mag1,rx1,ry1,x01,y01,ang1,sep1,besarg1,slp1];
mag2 = app.Magnitude2EditField.Value;
rx2 = app.xsize2EditField.Value;
ry2 = app.ysize2EditField.Value;
x02 = app.x02EditField.Value;
y02 = app.y02EditField.Value;
ang2 = app.Angle2EditField.Value;
sep2 = app.DiffractedSeparation2EditField.Value;
besarg2 = app.DiffractedBessel2EditField.Value;
slp2 = app.DiffractedSlope2EditField.Value;
params2guess = [mag2,rx2,ry2,x02,y02,ang2,sep2,besarg2,slp2];
bg = app.BackgroundEditField.Value;
paramsGlobalguess = [bg];
%and a whole bunch more
end
I completely understand how this may seem totally baffling to people, but the above formatting is how I prefer it. most of that could be collapsed into a single line of code using deal but I find such code difficult to read.
Hence, producing the same block of code, but with the right-hand-side and left-hand-side of each assignment swapped, is not nearly as easy as it is with deal since i cannot just copy and paste the stuff inside brackets/parentheses.
About the abitlity to fold your code: you can also use sections (%%) to fold your code, which will avoid the perfomance hit (however minimal) and the indententation of your code.
I don't think the %% works in App Designer
edit: this is no longer ture if it was true when I made that comment
That might be the case. I never use AppDesigner, but I always create GUIs in the normal editor (and I would do so for a class-based GUI as well).

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!