how to save my 3d matrix in X,Y,Z coordinates

조회 수: 18 (최근 30일)
Mohannad Ajamieh
Mohannad Ajamieh 2022년 5월 11일
답변: arushi 2024년 9월 6일
I have 134* 2d matrix (86*86 each) and they make 86*86*134 matrix.
I need to generate a point cloud of this matrix and that is why I must save the Matrix in xyz coordinates.
how can I save the matrix (I) in xyz table
my matrix (I) was generated by the following code
clear all
close all
names=dir('inter the name of your file\*.tiff');
for iii=1:size(names,1)
I(:,:,iii)=imread(strcat('inter the name of your file\',names(iii).name));
for j=1:size(names,1)
Img_3D=surface('XData',[0 355;0 355],'YData',[0 0;355 355],'ZData',[29.85*j 29.85*j;29.85*j 29.85*j], 'CData' ,flipdim(im2double(I(:,:,iii)),1),'FaceColor','texturemap','EdgeColor','none');
colormap(gray)
xlabel('x')
ylabel('y')
zlabel('z µm')
end
end

답변 (1개)

arushi
arushi 2024년 9월 6일
Hi Mohannad,
I understand that you want to convert a 3d matrix into XYZ coordinates.
To save the matrix `I` in XYZ coordinates, you can use the `writematrix` function in MATLAB. Here is how you can save the matrix in an XYZ table format:
% Assuming I is your 3D matrix of size 86x86x134
% Reshape the matrix to a 2D matrix of size 7396x134
I_reshaped = reshape(I, [], size(I, 3));
% Generate XYZ coordinates
[X, Y, Z] = meshgrid(1:size(I, 1), 1:size(I, 2), 1:size(I, 3));
% Reshape the coordinates to a 2D matrix of size 7396x134
X_reshaped = reshape(X, [], size(I, 3));
Y_reshaped = reshape(Y, [], size(I, 3));
Z_reshaped = reshape(Z, [], size(I, 3));
% Combine the coordinates and intensity values
xyz_data = [X_reshaped(:), Y_reshaped(:), Z_reshaped(:), I_reshaped(:)];
% Save the data as an XYZ table
% Combine the coordinates and data into a matrix
xyz_data = [X_reshaped, Y_reshaped, Z_reshaped, data_reshaped];
% Save the data as a space-separated text file
dlmwrite('data.txt', xyz_data, 'delimiter', ' ');
In this code, the `I` matrix is reshaped into a 2D matrix, and XYZ coordinates are generated using `meshgrid`. Then, the XYZ coordinates and intensity values are combined into a single matrix `xyz_data`. Finally, the `writematrix` function is used to save the data as an XYZ table, where each row represents a point with its XYZ coordinates and intensity value.
You can adjust the file name and delimiter (`' '`) according to your requirements. Optionally, you can specify column headers in the XYZ table by providing the `VariableNames` argument in the `writematrix` function.
Please refer to the following MATLAB documentation to know more about the ‘meshgrid,’ ‘dlmwrite’ and ‘reshape’ functions:
Hope this helps.

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by