How to plot 4D data from set of points (or from vectors)

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

Please upload a sample data file for us to test our code with: edit your question, click the paperclip button, then both the Choose file and Attach file buttons.

Sign in to comment.

Answers (1)

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

1 Comment

Thank You for this answer. Unfortunately, it doesn't work. The information returned from program about mistakes:
Error using interp3 (line 146)
Input grid is not a valid MESHGRID.
Error in slice (line 100)
vi = interp3(x,y,z,v,xi,yi,zi,method);
Error in Example (line 16)
slice(I, J, K, w, [], 1.234, []); %example, slice at J = 1.234
where 'Example' is your code.

Sign in to comment.

Asked:

on 23 Dec 2015

Edited:

on 13 Jan 2016

Community Treasure Hunt

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

Start Hunting!