how to rename variables using script/code vs doing it manually

18 views (last 30 days)
Short story: I want rename several variables via my script/code instead of having to manually do it from the workspace.
Long story: I have 20 different subjects and after importing data from each of them and massaging this data I end up with the variables that I want: Accelerations (ax, ay, az) and angular velocities (vx, vy, vz). I will eventually want to plot the ax from subject 1 against the ax from all other subjects; same goes for all other accelerations and velocities.
After rotation matrices and smoothing is applied to each subject data I get ax, ay...vz, but would like to now rename these to have a "tag" that says ax_sub1, ay_sub1 etc so that when I run the next subject through all the filters I don't have multiple variables with the same name--or have the variable rewritten with different data.
If I manually type ax_sub1 = ax then that works but don't want to do that since 20 subjects will soon turn into 100 (times six variables).
I've tried sprintf: nametag = input('input subject # and test # for tag: ','s')
sprintf('ax%s_',nametag) = ax; but get this error: Subscripted assignment dimension mismatch.
Have tried using the input fxn/command to directly type in an new name but that also doesn't work: input('input new var #')=ax; error: In an assignment A(I) = B, the number of elements in B and I must be the same.
Any assistance or suggestions to make this process go smoother would be very much appreciated. Thanks in advance for your help and time.

Answers (2)

SL B
SL B on 20 May 2013
Instead of having so many variables why don't you store them in a matrix?
A = [ax, ay, az;
ax_sub1, ay_sub1, az_sub1;
ax_sub2, ay_sub2, az_sub2] %etc
Unless their is a particular reason you want to store that many variables. This will be better for plotting later too I would imagine.
  2 Comments
Nahir
Nahir on 20 May 2013
Hi, I don't necessarily need to store all the variables so a Matrix structure would be okay. However, my issue is not being able to automatically differentiate between ax_sub1 and ax_sub2. That is, I'm using the same script to obtain data from subject 2 as I am for subject 1. So, once the script produces the first set of variables I need them to be renamed so that they do not get overwritten once I run the script again for the subsequent subject files.
I run the script once and get: ax ay az run it twice and get: ax, ay az overwritten with subject 2's data Am I making any sense?
SL B
SL B on 20 May 2013
Yeah I think I understand what you are staying. They would get over writing in a matrix which is not true if you are coding it correctly. I know you have some sort of outer loop going, if it is true that for each iteration you will be getting values that need to be stored then:
for i = 1:n
%calculations for ax, ay, az
A(i,:) = [ax, ay, ax]; %<--these are the values you calculate that
are now stored in row i of your matrix
end
This is assuming you will know the total number of rows you will be calculating. Otherwise you can you a counter.
counter = 1;
for i = 1:n
%calculations for ax, ay, az
A(counter,:) = [ax, ay, ax]; %<--these are the values you calculate
that are now stored in row i of your matrix
counter = counter+1;
end

Sign in to comment.


Walter Roberson
Walter Roberson on 20 May 2013

Community Treasure Hunt

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

Start Hunting!