Problem with XVariable property in stackedplot
3 views (last 30 days)
Show older comments
Hey,
I am new to the forum so please bear with me :)
I am trying to get the subplot to display the actual label of the row when I hover over it (see the blue number at the top of the uppermost plot in the image).
The names of the rows (here blood metabolite values) are stored in the following two structures (first is a string array and second is a table)
metabolite_labels = string(data.Properties.VariableNames(4:width(data)))'
lincorr_hkt = array2table(hkt_corr_res,...
'VariableNames',{'a' 'm' 'R2' 'rho' 'p-value' 'Significant?' 'Strength' 'Missing Values (%)'},...
'RowNames',metabolite_labels)
I have tried using metabolite_label as a string array as well as the RowName property from the lincorr_hkt table, both give me the same error. I dont understand what type it needs to be?
figure
s = stackedplot(lincorr_hkt,'XVariable',lincorr_hkt.Properties.RowNames)
Error using stackedplot (line 100)
'XVariable' value must be a character vector, a string scalar, a positive integer, or a logical array with one true
element.
figure
s = stackedplot(lincorr_hkt,'XVariable',metabolite_labels)
Error using stackedplot (line 100)
'XVariable' value must be a character vector, a string scalar, a positive integer, or a logical array with one true
element.
Any help would be much appreciated :)
2 Comments
Rik
on 15 Mar 2021
Since your x-variable seems to be a categorical array, instead of a numeric array, you could try to set them as tick labels. That will make the axis look very messy, so you might need to set the font to something very small to hide the text.
Accepted Answer
Adam Danz
on 16 Mar 2021
Edited: Adam Danz
on 16 Mar 2021
Here's an undocumented hack that adds a listener that responds to changes to the x-label at the top of the vertical reference line and uses a lookup table to add the label associated with the x-value. The example uses random 1x3 strings for x-labels. Follow the steps below.
1. Create stacked plot
The stacked plot can be created by any valid syntax and does not need to use a table as in this example.
sph is the stackedplot handle.
% Create stacked plot
fig = figure();
T = array2table(rand(100,5),'VariableNames',{'A','B','C','D','E'});
sph = stackedplot(T);
2. Create a lookup table to define labels for each x-value
lableLookupTbl is an nx2 table with headers 'x' and 'label' (lower case) defining the x values and their associated labels.
lableLookupTbl.x - Unique x values used by stackedplot (numeric, logical, datetime, or duration as of r2021a).
lableLookupTbl.label - cell array of character vectors or string array.
The x values might be row numbers of your table and the labels might be RowNames. This example uses random 1x3 strings.
% Create lookup table
labelLookupTbl = table((1:height(T))', mat2cell(char(randi([65,90],100,3)),ones(100,1),3), ...
'VariableNames', {'x','label'}); % cellstr or str; x unq vals
3. Create listener
This section uses undocumented methods to access the stackedplot datacursor object and assigns a listener that responds to changes to the x-label at the top of the vertical reference line in stackedplot.
% Get DataCursor handle
origState = warning('query', 'MATLAB:structOnObject');
cleanup = onCleanup(@()warning(origState));
warning('off','MATLAB:structOnObject')
S = struct(sph); % Undocumented
stackedDataCursor = struct(S.DataCursor); % Undocumented
clear('cleanup')
% Define listener
fig.UserData.Listener = addlistener(stackedDataCursor.DatatipLabels{end},'String',...
'PostSet',@(~,~)addRowLabelFcn([],[],stackedDataCursor,labelLookupTbl));
function addRowLabelFcn(~, ~, stackedDataCursor, labelLookupTbl)
% Listener that responds to changes to the datacursor obj that updates
% the x-label in stackedplot; stored in parent figure.
% stackedDataCursor: datacursor object in stackedplot
% labelLookupTbl: nx2 table with headers 'x','label' where x are unique,
% numeric values and label are cellstr or string arrays.
% Get current x-coordinate and associated label
x = stackedDataCursor.CursorLine.VertexData(1);
[~, row] = min(abs(labelLookupTbl.x - x));
label = [stackedDataCursor.DatatipLabels{end}.String, ' ', labelLookupTbl.label{row}];
% Update datacursor string
stackedDataCursor.DatatipLabels{end}.String = label;
end
3 Comments
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!