How to save and read a 3D matrix in MATLAB?
158 views (last 30 days)
Show older comments
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 Comments
James Tursa
on 20 Oct 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.
Accepted Answer
David Hill
on 20 Oct 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)
More Answers (1)
dpb
on 20 Oct 2022
Edited: dpb
on 20 Oct 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...
writematrix(data,'data.txt','Delimiter',',')
type data.txt
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
[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')
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,[]))
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.
See Also
Categories
Find more on Data Import and Export 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!