3D matrix with value of each point convert to .txt file

6 views (last 30 days)
Hi, I have a matrix which is 201*150*100 with value on each point. Right now I'm trying to convert the matrix to .txt. The file is of latitude, longitude, and depth. The ideal format for the .txt file to should be in columns with the shape of lat, long, depth, value. And then the next lane with the same format. If the lat, long, depth can start from 20 with interval increasing 0.1 instead of 0 all the way it will be perfect. Thanks in advance, please let me know if I confused you anywhere.
  1 Comment
luchen li
luchen li on 2 Nov 2017
If the column can be "20 20 20 value", then "20.1 20 20 value","20.2 20 20 value"....... until "40 20 20 value" (after 201 dots), then "20 20.1 20 value", "20 20.1 20 value" .......until "20 35 20 value"(after 151 dots), then "20.1 20.1 20.1 value".... it will be perfect. Thanks again

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 2 Nov 2017
[r, c, p] = size(YourMatrix);
[R, C, P] = ndgrid(20 + (0:r-1)*0.1, 20 + (0:c-1)*0.1, 20 + (0:p-1)*.1);
temp = [R(:), C(:), P(:), YourMatrix(:)];
fid = fopen('YourOutputFile.txt', 'wt');
fprintf(fid, '%g %g %g %g\n', temp.' ); %transpose is important
fclose(fid);
  1 Comment
luchen li
luchen li on 2 Nov 2017
This is amazing! Thanks for your quick response! My first question got solved in an hour after troubled me for a week. Really appreciate your help!!!

Sign in to comment.

More Answers (1)

KL
KL on 2 Nov 2017
Why not save each page in separate files? It's much simpler,
for k=1:size(yourMatrix, 3)
A = yourMatrix(:,:,k);
save(sprintf('matrix_page_%d.txt', k), 'A', '-ASCII', '-double');
end
  1 Comment
luchen li
luchen li on 2 Nov 2017
Thanks, I've tried that one, but the that will create 101 text files. However, the reason for this is trying to convert data to Gocad, which is a product of Paradigm, and I don't think the software can handle 101 txt files input...

Sign in to comment.

Categories

Find more on Data Type Conversion 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!