Data Tip addition not working

1 Ansicht (letzte 30 Tage)
Elena Casiraghi
Elena Casiraghi am 4 Nov. 2019
Kommentiert: Elena Casiraghi am 4 Nov. 2019
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!

Akzeptierte Antwort

Jan Studnicka
Jan Studnicka am 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');

Weitere Antworten (0)

Kategorien

Mehr zu Call C++ from MATLAB finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by