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
3D matrix with value of each point convert to .txt file
6 views (last 30 days)
Show older comments
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.
Accepted Answer
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);
More Answers (1)
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
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!