How to plot 4D data from set of points (or from vectors)
2 views (last 30 days)
Show older comments
I have format of 4D data such is used by Gnuplot when it draws plot. I need universal code, which can plot such format. For example, I need to draw data produces by this code:
p1=fopen('jakos.txt','w');
for i=-2:0.3:1
for j=-3:0.2:2
for k=2:0.1:3
fprintf(p1,'%f\t%f\t%f\t%f\n',i,j,k,i*exp(-i^2-j^2-k^2));
end
end
end
fclose(p1);
It produces this:
-2.000000 -3.000000 2.000000 -0.000000
-2.000000 -3.000000 2.100000 -0.000000
-2.000000 -3.000000 2.200000 -0.000000
[...]
-2.000000 -2.000000 2.900000 -0.000000
-2.000000 -2.000000 3.000000 -0.000000
-2.000000 -1.800000 2.000000 -0.000026
-2.000000 -1.800000 2.100000 -0.000017
[...]
1.000000 2.000000 2.800000 0.000003
1.000000 2.000000 2.900000 0.000002
1.000000 2.000000 3.000000 0.000001
I had an idea that after reading this data I can split each column to separate vectors as axis x, y, z and value but after this I've stuck. I don't know even how to create meshgrid from it. At first, I wanted to use slice function to represent data and do it in a loop, but it requires values in form of a matrix, which I don't know how to create from fourth column with values.
How can I draw this data (datas in such format)? If it is possible, I would still prefer represent this by slice, but any other working function will be good.
Thank You in advance
1 Comment
Answers (1)
Walter Roberson
on 23 Dec 2015
fid = fopen('jakos.txt', 'rt');
datacell = textscan(fid, '%f%f%f%f', 'Delimiter', '\t', 'CollectOutput', 1);
fclose(fid);
data = datacell{1};
ui = unique(data(:,1), 'stable'); ni = length(ui);
uj = unique(data(:,2), 'stable'); nj = length(uj);
uk = unique(data(:,3), 'stable'); nk = length(uk);
w = data(:,4);
w = permute( reshape(w, nk, nj, ni), [3 2 1] );
[I, J, K] = ndgrid(ui, uj, uk);
slice(I, J, K, w, [], 1.234, []); %example, slice at J = 1.234
See Also
Categories
Find more on Annotations 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!