Error adding DataTipRows to patch/fill

52 visualizaciones (últimos 30 días)
LS
LS el 30 de Jun. de 2022
Comentada: LS el 3 de Jul. de 2022
I'm trying to add a new row to the data tip for some fill/patch regions in one of my plots.
I've successfully done the same thing to plot/scatter in the same figure, where the new row is a datetime.
Trying to do the same for a fill, I found I needed to first use datatip to create the correct field. It had previously been giving me the error:
"Unrecognized method, property, or field 'DataTipTemplate' for class 'matlab.graphics.primitive.Patch'."
Following this, the error has become:
"Error using matlab.graphics.datatip.DataTipTemplate/set.DataTipRows
Value must be compatible with the data source."
The current code I have is as follows, with angles/coords and times removed.
figure; %Produce figure
LATLONG.F1 = [0 1 1 0; 1 0 0 1]; %2x4 array of angles, angles removed
LATLONG.F2 = [0 1 1 0; 1 0 0 1]; %2x4 array of angles, angles removed
LATLONG.F3 = [0 1 1 0; 1 0 0 1]; %2x4 array of angles, angles removed
plot_colours = winter; %Prepare array of colour values to use
t1 = [datetime('01-JAN-1970') datetime('01-JAN-1970') datetime('01-JAN-1970')]; %Datetime array, dates removed
t2 = [datetime('02-JAN-1970') datetime('02-JAN-1970') datetime('02-JAN-1970')]; %Datetime array, dates removed
field2 = fieldnames(LATLONG); %Get list of all fields in struct
for i = 1:length(field2) %For all fields, plot
%Plot region
plot2 = patch(LATLONG.(field2{i})(1,:),LATLONG.(field2{i})(2,:),plot_colours(10+5*(i-1),:),'FaceAlpha',0.05,'LineStyle','-.');
%Generate datatip
datatip(plot2,'Visible','off');
%Create dataTipTextRows
text_var1 = dataTipTextRow('Start time:',t1(i));
text_var2 = dataTipTextRow('End time:',t2(i));
%Add row to datatips
plot2.DataTipTemplate.DataTipRows(end+1) = text_var1; %ERROR OCCURS HERE
plot2.DataTipTemplate.DataTipRows(end+1) = text_var2;
%Update DisplayName for legend
plot2.DisplayName = append('Session: ',field2{i});
end
Error using matlab.graphics.datatip.DataTipTemplate/set.DataTipRows
Value must be compatible with the data source.
is there a way to get around this issue and have a row with a datetime in the datatip beneath the x/y coords?
Thanks.

Respuesta aceptada

Simon Chan
Simon Chan el 30 de Jun. de 2022
Try the following:
figure; %Produce figure
LATLONG.F1 = [0 1 1 0; 1 0 0 1]; %2x4 array of angles, angles removed
LATLONG.F2 = [0 1 1 0; 1 0 0 1]; %2x4 array of angles, angles removed
LATLONG.F3 = [0 1 1 0; 1 0 0 1]; %2x4 array of angles, angles removed
plot_colours = winter; %Prepare array of colour values to use
field2 = fieldnames(LATLONG); %Get list of all fields in struct
%
for i = 1:length(field2) %For all fields, plot
plot2 = patch(LATLONG.(field2{i})(1,:),LATLONG.(field2{i})(2,:),plot_colours(10+5*(i-1),:),'FaceAlpha',0.05,'LineStyle','-.');
%Generate datatip
t1=repelem(datetime('01-JAN-1970'),1,size(plot2.Vertices,1)); % Matches the number of vertices
t2=repelem(datetime('02-JAN-1970'),1,size(plot2.Vertices,1)); % Matches the number of vertices
datatip(plot2,'Visible','off');
%Create dataTipTextRows
text_var1 = dataTipTextRow('Start time:',t1);
text_var2 = dataTipTextRow('End time:',t2);
%Add row to datatips
plot2.DataTipTemplate.DataTipRows(end+1) = text_var1; %ERROR OCCURS HERE
plot2.DataTipTemplate.DataTipRows(end+1) = text_var2;
%Update DisplayName for legend
plot2.DisplayName = append('Session: ',field2{i});
end
  1 comentario
LS
LS el 3 de Jul. de 2022
So "Value must be compatible with the data source" was wanting there to be an equal number of tooltip points to data points. One for each of the vertices.
Your solution works perfectly, thanks.

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by