Data Tip addition not working

조회 수: 2 (최근 30일)
Elena Casiraghi
Elena Casiraghi 2019년 11월 4일
댓글: Elena Casiraghi 2019년 11월 4일
Dear, I'm trying to change the data tip of my plot and I get an error I relly can't understand
The code takes data from file: FertilitySoloCountries.xlxs (attached). Its a file reporting the fertility rate for each country (one country per row) and for 16 year intervals (on the column). I want to plot these trends. And I do with the following code.
close all;
clc
figPosition = [0 0 1 1];
data = readtable('FertilitySoloCountries.xlsx', 'PreserveVariableNames', true);
yearRange = data.Properties.VariableNames;
yearRange = yearRange(:,2:end);
countries = table2cell(data(:,1));
numCountries = numel(countries);
dataMat = table2array(data(:, 2:end));
numRange = size(dataMat,2);
numCols = 4;
numRows = floor(numRange/4);
meanEveryNation = mean(dataMat);
figSparks = figure('Name', 'With SparkLines', 'units','normalized','outerposition',figPosition);
hold on; plot(meanEveryNation, 'k--', 'LineWidth', 2);
colormap
for nC = 1: numel(countries)
hold on; h = plot(dataMat'); %legend(countries);
addTip = dataTipTextRow('country',countries{nC});
h.DataTipTemplate.DataTipRows(end+1) = addTip;
end
ax = gca;
ax.XTickLabelRotation = 90;
set(ax,'XTick',1:numel(yearRange), 'XTickLabel', yearRange, 'fontsize', 6);
saveas(figSparks, 'PlotPerRange.jpg');
The code worked until I wanted to add a data tip. When I do that, matlab says:
Expected one output from a curly brace or dot indexing expression, but there were 209 results.
Error in PlotFertilitaPulito (line 23)
h.DataTipTemplate.DataTipRows(end+1) = addTip;
Where am I making it wrong?
I have been using datatips in another work... and it worked!

채택된 답변

Jan Studnicka
Jan Studnicka 2019년 11월 4일
close all;
clc
figPosition = [0 0 1 1];
data = readtable('FertilitySoloCountries.xlsx', 'PreserveVariableNames', true);
yearRange = data.Properties.VariableNames;
yearRange = yearRange(:,2:end);
countries = table2cell(data(:,1));
numCountries = numel(countries);
dataMat = table2array(data(:, 2:end));
numRange = size(dataMat,2);
numCols = 4;
numRows = floor(numRange/4);
meanEveryNation = mean(dataMat);
figSparks = figure('Name', 'With SparkLines', 'units','normalized','outerposition',figPosition);
hold on; plot(meanEveryNation, 'k--', 'LineWidth', 2);
colormap
h = plot(dataMat');
for nC = 1: numel(countries)
addTip = dataTipTextRow('country',repmat(string(countries{nC}),16,1));
h(nC).DataTipTemplate.DataTipRows(end+1) = addTip;
end
ax = gca;
ax.XTickLabelRotation = 90;
set(ax,'XTick',1:numel(yearRange), 'XTickLabel', yearRange, 'fontsize', 6);
saveas(figSparks, 'PlotPerRange.jpg');

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by