Take the 2-minute tour ×

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]
[X,V] = meshgrid(X,V);
Z = Capture.*X

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:


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?


share|improve this question

1 Answer 1

up vote 3 down vote accepted

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

#% Step 4: set the custom function as the updatefcn, and give it the extra...
#% data to be displayed

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.

share|improve this answer
Thanks tmpearce for the response, I knew about this solution, the problem is that it doesn't give me precise data, and rather than a color bar, I would rather have the exact element of V at that point. Is this possible? –  Tim May 25 '12 at 3:33
Ah, I see. Updated the answer with info about custom datatips. –  tmpearce May 25 '12 at 15:57
tmpearce, thanks so much for your response, this worked perfectly, I am not sure I understand it too well, but it might serve me best to understand the code for data cursor without any additions first. Thanks again! –  Tim May 25 '12 at 16:38

Your Answer


