1

I have created a fitted surface from x, y, z data points. How do I insert data tips for the min and max value in the chart?

defDM_fit = fit([def_X, def_Y],def_Z,'cubicinterp');
clf;
figure(2)
plot(defDM_fit,[def_X, def_Y],def_Z);

using the following test code is raising an error "Invalid argument. The object has been deleted or does not support data tips":

datatip(defDM_fit, def_X(1), def_Y(1), def_Z(1))

And I do not know how to manage that tips show up at min and max value in the chart by code.

enter image description here

Adriaan
  • 17,741
  • 7
  • 42
  • 75
JFS
  • 2,992
  • 3
  • 37
  • 48
  • 1
    Have you tried adding the datatips to the `plot` and not to the `fitobject` object? (i.e. `p = plot(defDM_fit,[def_X, def_Y],def_Z); datatip(p, def_X(1), def_Y(1), def_Z(1)) `). I cannot check because your example is not [Reproducible](https://stackoverflow.com/help/minimal-reproducible-example) – BillBokeey Nov 04 '22 at 11:04
  • @BillBokeey, thanks. I did and it will lead to the same error. – JFS Nov 04 '22 at 11:25
  • 2
    Can you add data to make the example reproducible, so that I can try? I am pretty sure it is just a matter of object types – BillBokeey Nov 04 '22 at 11:42
  • @BillBokeey, give me a second... – JFS Nov 04 '22 at 12:25

2 Answers2

3

Plotting a fitted surface created with fit outputs a 2x1 graphics array. The first element is the surface (Surface object), the second element is a Line object that holds the points on which your data was fitted. In order to add datatips, you will have to use one of these two objects, and more probably the Surface object, for example:

load franke
T = table(x,y,z);

f = fit([T.x, T.y],T.z,'linearinterp');
p = plot( f, [T.x, T.y], T.z );

datatip(p(1),T.x(1),T.y(1),T.z(1))

enter image description here

BillBokeey
  • 3,168
  • 14
  • 28
  • cool it works with `p2 = plot(defDM_fit,[def_X, def_Y],def_Z);` and `datatip(p2(1),def_X(1), def_Y(1), def_Z(1))` I try to find the indexes for the min and max def_Z values. Thanks! – JFS Nov 04 '22 at 12:34
2

The first argument of datatip is a graphic object, not a surface/line fit object.

defDM_fit = fit([def_X, def_Y],def_Z,'cubicinterp');
figure(2)
p=plot(defDM_fit,[def_X, def_Y],def_Z);
datatip(p, def_X(1), def_Y(1), def_Z(1))

There are many other things that may be wrong with your approach, particularly because you are showing a surface plot (surf) in your example, yet your code uses a line plot (plot). I am not even sure what the arguments even are, the way you are putting them.

Consider reading the documentation of the functions you are using, as they come with examples on how to use them: https://uk.mathworks.com/help/curvefit/fit.html

Wolfie
  • 27,562
  • 7
  • 28
  • 55
Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • I did use exactly the documentation from your link. I use the `fit` function to generate the fitted surface as in the docs. Maybe the code line `datatip(defDM_fit, def_X(1), def_Y(1), def_Z(1))` is wrong. I do not know how to use it right to create data tips within my chart. – JFS Nov 04 '22 at 12:14
  • but thanks for the hint to the graphic object. – JFS Nov 04 '22 at 12:23
  • 1
    By the way the `plot` function I use is from the Curve Fitting Toolbox and plots `cfit` and `sfit` objects. So, `plot` is not only for line plots. See [HERE](https://www.mathworks.com/help/releases/R2022a/curvefit/cfit.plot.html?s_tid=doc_ta). – JFS Nov 05 '22 at 08:38