How to save and read a 3D matrix in MATLAB?

122 visualizaciones (últimos 30 días)
Jamie Al
Jamie Al el 20 de Oct. de 2022
Comentada: Jamie Al el 26 de Oct. de 2022
I am sort of confused how to go about this issue. I am trying to write a function in C++ that saves a 3D matrix in a text file and can be read by MATLAB for 3D plotting purposes. So as a test I am trying to save the 3D matrix in a text file using MATLAB first. I have few attempts by MATLAB and honestly I am not sure if the method I am using is the problem or the readmatrix function in MATLAB is set to read 2D matrices only.
My attempts:
m = 4; n = 3 ; p = 4 ; % dimensions for matrix
A = rand(m,n,p); % make a dummy 3d matrix
F = [repmat(' %0.4f',1,n),'\n']; % make the required format
fid = fopen('test.txt','w') ; % open file to write
% loop to write each 2D matrix
for i = 1:p
fprintf(fid,F,A(:,:,i).') ; % write the p'th matrix
fprintf(fid,'\n') ; % give empty space after a matrix is written
end
TEST = readmatrix('test.txt');
Now this reads as a 16x3 instead of a 4x4x3. I also tried using the
writematrix(A,'3DMat.txt');
This also produces the same problem. I guess I want something like the same format of thi example:
x = (0:Nx-1)/Nx*2*pi;
y = (0:Ny-1)/Ny*2*pi;
z = (0:Nz-1)/Nz*2*pi;
[X,Y,Z] = meshgrid(x,y,z);
Function = %Function in X,Y,Z
figure
isosurface(X,Y,Z,Function);
None of the previous examples are readable in such a way that I can plot as 3D isosurface plot because it's being written as 2D matrix instead.
  2 comentarios
James Tursa
James Tursa el 20 de Oct. de 2022
Why do you want to use a text file for this? It would be much easier and faster to write and read the file as binary numbers. Or connect your C++ code to MATLAB via mex or engine and send the data back & forth directly instead of using files.
Jamie Al
Jamie Al el 20 de Oct. de 2022
I am using all these different libraries with C++ code such as Eigen and Eigen tensor and it seems to me the simplest option is to get a text file. A binary output sounds fine too but I can't even print the matrix correctly.

Iniciar sesión para comentar.

Respuesta aceptada

David Hill
David Hill el 20 de Oct. de 2022
m = 4; n = 3 ; p = 4 ; % dimensions for matrix
A = round(rand(m,n,p),4);
writematrix(A,'3DMat_4_3_4.txt');
a=reshape(readmatrix('3DMat_4_3_4.txt'),4,3,4);
isequal(a,A)
ans = logical
1

Más respuestas (1)

dpb
dpb el 20 de Oct. de 2022
Editada: dpb el 20 de Oct. de 2022
"readmatrix(filename) creates an array by reading column-oriented data..."
A matrix in MATLAB is 2D; writematrix writes the planes of a 3D array as appending each subsequent plane on the right of the first. See following example to see what happens...
data=pagetranspose(reshape(1:24,3,4,2)) % so can see who's who in the zoo...
data =
data(:,:,1) = 1 2 3 4 5 6 7 8 9 10 11 12 data(:,:,2) = 13 14 15 16 17 18 19 20 21 22 23 24
writematrix(data,'data.txt','Delimiter',',')
type data.txt
1,2,3,13,14,15 4,5,6,16,17,18 7,8,9,19,20,21 10,11,12,22,23,24
Now, obviously, when you read back in, "Yes, Virginia, you will get a 2D matrix." There's nothing in the file to tell you it's 3D.
Now, that's also true in general in text files from any language, when you write out
delete data.csv
Warning: File 'data.csv' not found.
[r,c,p]=size(data);
fmt=[repmat('%.0f,',1,c-1) '%.0f\n'];
fid=fopen('data.csv','w');
for i = 1:p
fprintf(fid,fmt,data(:,:,i).');
end
fclose(fid);
%dir('data.csv')
new=readmatrix('data.csv')
new = 8×3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
The output to the file is still a 2D array; there's no indication of any 3D content in the file, only in how you read the data back in can you recreate the 3D shape or by reshape-ing it after reading.
new=pagetranspose(reshape(readmatrix('data.csv').',3,4,[]))
new =
new(:,:,1) = 1 2 3 4 5 6 7 8 9 10 11 12 new(:,:,2) = 13 14 15 16 17 18 19 20 21 22 23 24
What is typically done is to write the dimensions of the array at the beginning of the file so can know what they are on reading back in.
IF you are staying only in MATLAB and not taking it somewhere else, the easiest way by far is to just use the @doc:save and load functions -- they keep all this stuff internally are return the same shape as was when written when reloaded.

Community Treasure Hunt

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

Start Hunting!

Translated by