Calculate the coordinates of nodes in a plot3

Hi. I would like to know how to calculate the coordinates of the nodes (x,y,z coordinates) and the total number of nodes in this figure. Attached code and figure. Thanks!

 Accepted Answer

This is one figure in the x-y plane. The z coordinates should be constant, wherever you choose to set z. If you had several images for a stack, you could use code.m as intended.
imageArray = im2uint8(imread('figure.png'));
imagesc(imageArray)
CoordinateMatrix = pic2points(imageArray);
totalNodes = size(CoordinateMatrix,1)
totalNodes = 570
z = 1;
coordinateMatrix = [CoordinateMatrix, z.*ones(totalNodes,1)];

4 Comments

Thank you for the answer @Chris!
But if I have two or more figures. We can consider two equal figures 'figure.png'.
In this case how can I keep the coordinate calculation and also update the 'totalNodes' (considering both figures)?
I added the lines of code
coordinate_x = CoordinateMatrix(:, 1);
coordinate_y = CoordinateMatrix(:, 2);
coordinate_z = current_z;
coordinate_matrix = [coordinate_x, coordinate_y, coordinate_z];
totalNodes = size(coordinate_matrix, 1);
inside the for loop but it did not work.
@Alberto Acri You could build a 3D array:
coordinate_matrix(:,:,k) = [coordinate_x, coordinate_y, coordinate_z];
More than 2 dimensions can be difficult to visualize, but it is fast in Matlab and 3D makes sense for a stack of images.
You could also use a cell array:
coordinate_matrix{k} = [coordinate_x, coordinate_y, coordinate_z];
where each image is kept in a different cell.
Or simply add the new set of coordinates to the end of the coordinate_matrix:
temp_matrix = [coordinate_x, coordinate_y, coordinate_z];
coordinate_matrix = [coordinate_matrix; temp_matrix];
I tried your codes.
I exclude the first code.
The second code creates me the two cells correctly in 'coordinate_matrix' (as I have two images), but it does not update 'totalNodes'. How can I do this?
coordinate_x = CoordinateMatrix(:, 1);
coordinate_y = CoordinateMatrix(:, 2);
coordinates_z = current_z;
coordinate_matrix{k} = [coordinate_x, coordinate_y, coordinate_z];
totalNodes = size(coordinate_matrix{k},1);
The third code creates me the matrix 'coordinates_matrix_1' the way I prefer to see it on the workspace and also counts the 'totalNodes', but the number that is generated is wrong. Considering the two attached figures, the sum is 570 (for 'figure.png') + 698 (for 'figure1.png') = 1268. The code, however, gives me a different value: 1396. How come?
coordinate_x = CoordinateMatrix(:, 1);
coordinate_y = CoordinateMatrix(:, 2);
coordinates_z = current_z;
coordinate_matrix = [coordinate_x, coordinate_y, coordinate_z];
temp_matrix = [coordinates_x, coordinates_y, coordinates_z];
coordinate_matrix_1 = [coordinate_matrix; temp_matrix];
totalNodes = size(coordinate_matrix_1, 1);
I thank you if you can give me an answer for both cases.
For plot3, the third method is best. However, you have introduced a new coordinate_matrix_1, which is overwritten with each loop. Delete that. Additionally, you will need to initialize the empty matrix before the loop.
coordinate_matrix = [];
for k = %...
%...
temp_matrix = [coordinate_x, coordinate_y, coordinate_z];
coordinate_matrix = [coordinate_matrix; temp_matrix];
end
totalNodes = size(coordinate_matrix, 1);
If you only want the total number of nodes for the entire stack, you can move the "totalNodes" calculation to after the end of the loop.
For the cell array, you can calculate the number of nodes with this:
totalNodes = size(cat(1,coordinate_matrix{:}),1);

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Release

R2021b

Asked:

on 12 Nov 2022

Commented:

on 13 Nov 2022

Community Treasure Hunt

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

Start Hunting!