4D plot display variables with data cursor Matlab

3.9k views Asked by At

I am having trouble figuring out how display 4 variables in my plot.

I want to vary the independent variables X,V, to produce the dependent variables Y and Z. Y is a function of X AND V. And Z is a function of Y AND X.

This may be easier to see the dependencies: X, V, Y(X,V), Z(X,Y(X,V)).

I have used the surf function to plot X,Y,Z, but I also want to know the values of V, which I cannot currently ascertain.

Here is some test data to illustrate:

X = linspace(1,5,5)
V = linspace(1,5,5)
Capture = []
for j = 1:length(V)
Y = X.*V(j)
Capture = [Capture;Y]
end
[X,V] = meshgrid(X,V);
Z = Capture.*X
surf(X,Y,Z)

If I use the data cursor, I can see values of X,Y,Z, but I would also like to know the values of V. I know that the way I have it set up is correct because if I make two plots, say:

surf(X,Y,Z)
surf(X,V,Z) 

and then use the data cursor to go on the same point of X and Z for both graphs the values for V and Y are what they should be for that point (X,Z).

Is there anyway to show the values for X,Y,V and Z without having to generate two separate graphs?

Thanks!

1

There are 1 answers

3
tmpearce On BEST ANSWER

Using color as your 4th dimension is a possibility (whether it looks good to you is a matter of taste).

surf(X,Y,Z,V); #% 4th arg (V) is mapped onto the current colormap

You can change the colormap to suit your tastes.

colorbar #% displays a colorbar legend showing the value-color mapping

Edit: The questioner wants to see exactly the data in the not-shown array, rather than just a color. This is a job for custom data cursor function. Below I've implemented this using purely anonymous functions; doing it within a function file would be slightly more straightforward.

#% Step 0: create a function to index into an array...
#% returned by 'get' all in one step  
#% The find(ismember... bit is so it returns an empty matrix...
#% if the index is out of bounds (if/else statements don't work...
#% in anonymous functions)
getel = @(x,i) x(find(ismember(1:numel(x),i)));

#% Step 1: create a custom data cursor function that takes...
#% the additional matrix as a parameter
myfunc = @(obj,event_obj,data) {...
['X: ' num2str(getel(get(event_obj,'position'),1))],...
['Y: ' num2str(getel(get(event_obj,'position'),2))],...
['Z: ' num2str(getel(get(event_obj,'position'),3))],...
['V: ' num2str(getel(data,get(event_obj,'dataindex')))] };

#% Step 2: get a handle to the datacursormode object for the figure
dcm_obj = datacursormode(gcf);

#% Step 3: enable the object
set(dcm_obj,'enable','on')

#% Step 4: set the custom function as the updatefcn, and give it the extra...
#% data to be displayed
set(dcm_obj,'UpdateFcn',{myfunc,V})

Now the tooltip should display the extra data. Note that if you change the data in the plot, you'll need to repeat Step 4 to pass the new data into the function.