
Error adding DataTipRows to patch/fill
39 次查看(过去 30 天)
显示 更早的评论
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
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.
0 个评论
采纳的回答
Simon Chan
2022-6-30
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

更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!