How to use the command window in order to make a script faster and interactive?
Show older comments
Hi everyone! I would like to make a code more interactive using the command window.
I have some variables .mat named new_x_edges_50 , new_y_edges_50, new_z_edges_50, new_x_edges_60, new_y_edges_60, new_z_edges_60, new_x_edges_70, new_y_edges_70, new_z_edges_70 and so on...
each of these variables have dimension 2xn.
In this code I would like that the user has the possibility to say that If z_start =70 (instead of z_start = 50 like in this code), then in dist_s I have to use new_x_edges_70, new_y_edges_70 and new_z_edges_70 (instead of new_x_edges_50, new_y_edges_50, new_z_edges_50), then xts = new_x_edges_70(ids) yts = new_y_edges_70(ids) zts = new_z_edges_70(ids).
at the same time if z_goal = 140 (instead of z_goal = 150 like in this code), then in dist_g I have to use new_x_edges_140, new_y_edges_140 and new_z_edges_140 (instead of new_x_edges_150, new_y_edges_150, new_z_edges_150), then xtg = new_x_edges_140(idg) ytg = new_y_edges_140(idg) ztg = new_z_edges_140(idg).
So, I would like the same code as I wrote it, but I don't want to change the script as I select a different z_start or z_goal, but I would like the user change it from the command window. How can I do it? Thanks in advance
x_start = -1079.57;
y_start = 501.714;
z_start = 50;
x_goal = -97.8654;
y_goal = 27.5153;
z_goal = 150;
dist_s = sqrt((new_x_edges_50 - x_start).^2+(new_y_edges_50 - y_start).^2+(new_z_edges_50 - z_start).^2);
dist_g = sqrt((new_x_edges_150 - x_goal).^2+(new_y_edges_150 - y_goal).^2+(new_z_edges_150 - z_goal).^2);
2 Comments
"I have some variables .mat named new_x_edges_50 , new_y_edges_50, new_z_edges_50, new_x_edges_60, new_y_edges_60, new_z_edges_60, new_x_edges_70, new_y_edges_70, new_z_edges_70 and so on... "
Ah, so you forced meta-data into your variable names. Well, that will make processing your data much harder, if you then try to access those variable names dynamically...
"In this code I would like that the user has the possibility to say that If z_start =70 (instead of z_start = 50 like in this code), then in dist_s I have to use new_x_edges_70, new_y_edges_70 and new_z_edges_70 (instead of new_x_edges_50, new_y_edges_50, new_z_edges_50), then xts = new_x_edges_70(ids) yts = new_y_edges_70(ids) zts = new_z_edges_70(ids)."
... like that.
Meta-data is data, and data belongs in variables, not in variable names. Once you do that, then the solution to your question will be simple (e.g. indexing, fieldnames, etc). But as posed, your data design forces you into writing slow, complex, inefficient, insecure, obfuscated code which is buggy and difficult to debug:
So far you have not told us the most important information: how did you get all of those variables into the workspace? That would be correct place to fix your code, e.g. by placing all of those arrays into one cell array. After that, you can use a simple numeric vector and some basic indexing to achieve your task (and much neater, simpler, and more efficient than you current approach using numbered variable names).
"How to use the command window in order to make a script faster..."
By using arrays and indexing, not by accessing variable names dynamically.
@Loren99: please upload the original data files, not one MAT file with all of your renamed variables in it.
The original data design (where every MAT file contains variables of exactly the same names) is the best, and makes this task simple and robust. By renaming all of the variables with numbers in their names, you made this task harder for me (and you).
Accepted Answer
More Answers (0)
Categories
Find more on Startup and Shutdown 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!