# 3D plot in app designer with for loop.

10 views (last 30 days)
Jacob Beesley on 19 Nov 2021
Answered: Cris LaPierre on 19 Nov 2021
I have wrote a Live Script that takes a .tsv file that reprisents two sets of X,Y,Z coordinates over n rows (nx6 matrix). Columns 1:3 are the first coordinate set and 4:6 are the next. The program finds the midpoint between the corresponding points of each coordinate set (e.g. midpoint between X coordinates, then Y, then Z). It also calculates the 3D distance between the two coordinate sets.
The purpose of this is to visualize the differene between the distance of the two coordinate sets from the .tsv file and the true known distance between the points. This has been done by looping through each of the n rows in the matrix and plotting them individually. If the .tsv file calculated distance is less than the true value, it will be plotted blue. If the calculated distance is greater than the true value, it will be plotted red, and if the two values are the same the plot will be black. Additionally, the marker size of the plot will depend on the size of the difference between the calculated and true value. I'll put this below. I have adapted the program to recieve .xlsx instead of .tsv in case anyone would like to try it out with the example data attached.
true_len = 602; % true distance between coordinates
mid_x = (d(:,1) + d(:,4)) / 2; % midpoints between each axis
mid_y = (d(:,2) + d(:,5)) / 2;
mid_z = (d(:,3) + d(:,6)) / 2;
len = sqrt((d(:,1)-d(:,4)).^2 + (d(:,2)-d(:,5)).^2 ...
+ (d(:,3)-d(:,6)).^2); % pythagoras for 3D distance of .xlsx/.tsv data
i = 1;
% blue = under measurement, red = over measurement, black = exact
for i = 1:height(d)
if len(i) < true_len
ms = (true_len - len(i)) * 50; % amplified marker size by 50 for better visualization
scatter3(mid_x(i),mid_y(i),mid_z(i),ms,'bo')
elseif len(i) > true_len
ms = (len(i) - true_len) * 50;
scatter3(mid_x(i),mid_y(i),mid_z(i),ms,'ro')
else
scatter3(mid_x(i),mid_y(i),mid_z(i),ms,'kx')
end
i = i + 1;
hold on
end
hold off
This works in the Live Script but when I adapt it for the App Designer it doesn't. I'll put this below. The app code is more or less the same but I allow the user to input the true distance into a numeric edit field and I obviously need to state the "app.UIAxes" in the scatter3 function.
true_len = app.WandLengthEditField.Value; % user can input their own known length (602 in this case)
mid_x = (d(:,1) + d(:,4)) / 2;
mid_y = (d(:,2) + d(:,5)) / 2;
mid_z = (d(:,3) + d(:,6)) / 2;
len = sqrt((d(:,1) - d(:,4)) .^2 + (d(:,2) - d(:,4)) .^2 ...
+ (d(:,3) - d(:,6) .^2));
i = 1;
for i = 1:height(d)
if len(i) < true_len
ms = (true_len - len(i)) * 50;
scatter3(app.UIAxes, mid_x(i), mid_y(i),...
mid_z(i), ms, 'bo')
elseif len(i) > true_len
ms = (len(i) - true_len) * 50;
scatter3(app.UIAxes, mid_x(i), mid_y(i),...
mid_z(i), ms, 'ro')
else
scatter3(app.UIAxes, mid_x(i), mid_y(i),...
mid_z(i), ms, 'kx')
end
hold on
i = i + 1;
end
hold off
end
I think there is an issue with the for loop as I am only recieving one single data point on the app.UIAxes. Additionally another blank figure pops up every time the program is run.
I just want the same plot to appear on the app that I made in the Live script. Thanks.

Cris LaPierre on 19 Nov 2021
I'm not exactly sure what you mean by 'doesn't work', but I'm guessing you only get the final scatter plot. In app designer, you need to specify the axes for hold as well.
hold(app.UIAxes,'on')
...
hold(app.UIAxes,'off')

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by