How to use the command window in order to make a script faster and interactive?

3 views (last 30 days)
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);
Stephen23 on 14 Sep 2022
Edited: Stephen23 on 14 Sep 2022
@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).

Sign in to comment.

Accepted Answer

Stephen23 on 14 Sep 2022
Edited: Stephen23 on 14 Sep 2022
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)
N = 4
C = cell(1,N);
for k = 1:N
F = sprintf('reticolato2D_%u.mat',V(k));
C{k} = load(F);
S = [C{:}] % using the same names makes this task easy, unlike your approach.
S = 1×4 struct array with fields:
new_x_edges new_y_edges new_z_edges
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:
ans = 60
ans = struct with fields:
new_x_edges: [2×871 double] new_y_edges: [2×871 double] new_z_edges: [2×871 double]
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)
A = struct with fields:
new_x_edges: [2×871 double] new_y_edges: [2×871 double] new_z_edges: [2×871 double]
B = S(ixB)
B = struct with fields:
new_x_edges: [2×27 double] new_y_edges: [2×27 double] new_z_edges: [2×27 double]
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);
Do NOT force meta-data into variable names!
Stephen23 on 15 Sep 2022
"Can I ask you to remove from your answer the attached data .mat?"
I replaced the numeric data with random values.

Sign in to comment.

More Answers (0)


Find more on Tables 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!

Translated by