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

1 次查看(过去 30 天)
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 个评论
luchen li
luchen li 2017-11-2
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

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2017-11-2
[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 个评论
luchen li
luchen li 2017-11-2
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!!!

请先登录,再进行评论。

更多回答(1 个)

KL
KL 2017-11-2
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 个评论
luchen li
luchen li 2017-11-2
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...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by