Data Tip addition not working

1 次查看(过去 30 天)
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