Data Tip addition not working

2 views (last 30 days)
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!

Accepted Answer

Jan Studnicka
Jan Studnicka on 4 Nov 2019
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');

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!