How to use the command window in order to make a script faster and interactive?
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2022-9-14
编辑:Stephen23
2022-9-14
"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.
Stephen23
2022-9-14
编辑:Stephen23
2022-9-14
@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).
采纳的回答
Stephen23
2022-9-14
编辑:Stephen23
2022-9-14
I had to write a script to carefully remove all of those numbers from the variable names and recreate the original MAT files according to the information you gave here (which shows that the variable names are exactly the same in each MAT file, as they should be for robust and efficient code). In future, please attach original data, not your renamed and altered data. Numbering the variable names makes this task much harder.
V = [50,60,70,150];
N = numel(V)
C = cell(1,N);
for k = 1:N
F = sprintf('reticolato2D_%u.mat',V(k));
C{k} = load(F);
end
S = [C{:}] % using the same names makes this task easy, unlike your approach.
Now all of the data are available in the structure S. You can trivially access any one file's data, e.g. the second file:
V(2)
S(2)
You can easily pick any two of those values and get all of the coresponding data from the structure:
wantA = 60; % select one value
wantB = 150; % select one value
ixA = V==wantA; % logical index
ixB = V==wantB; % logical index
A = S(ixA)
B = S(ixB)
Now you do whatever you want with A and B in your script. For example:
dist_s = sqrt((A.new_x_edges - x_start).^2+(A.new_y_edges - y_start).^2+(A.new_z_edges - z_start).^2);
dist_g = sqrt((B.new_x_edges - x_goal).^2 +(B.new_y_edges - y_goal).^2 +(B.new_z_edges - z_goal).^2);
[mns ids] = min(dist_s(:));
xts = A.new_x_edges(ids);
yts = A.new_y_edges(ids);
zts = A.new_z_edges(ids);
...etc
Do NOT force meta-data into variable names!
3 个评论
Stephen23
2022-9-15
"Can I ask you to remove from your answer the attached data .mat?"
I replaced the numeric data with random values.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!