how to rename variables using script/code vs doing it manually
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuestas (2)
SL B
el 20 de Mayo de 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 comentarios
SL B
el 20 de Mayo de 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
Ver también
Categorías
Más información sobre Whos en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!