How to stackedplot with text data

I have uit.Data and i want to use "stackedplot" to display the information from the table.
I want to plot below table x & y coordinates.
where x for WeirdDuration and y for text data

1 comentario

Adam Danz
Adam Danz el 2 de Dic. de 2019
While it's fairly straightforward to plot table data using the line below, I don't think adding text is supported.
To add text, you would need to supply the axes handle in order to specify which axes the test should be added to but stackedplot does not return axis handles so there's no way (I know of) to add things to a stakedplot axes. It's a fairly new feature (r2018b) so maybe that functionality will be added in the future.
Instead, I recommend creating your own subplot axes and use linkaxes to link the axis ranges. The vertical line won't appear but you could add grids so it's easy to visually compare subplots.

Iniciar sesión para comentar.

 Respuesta aceptada

Adam Danz
Adam Danz el 2 de Dic. de 2019
Editada: Adam Danz el 19 de Nov. de 2020
** UPDATE**
Almost a year later I learned of a way to add text to stackedplot axes. See this answer:
** Original answer showing alternative solution **
I'm assuming you're positioning text according to this screen shot you shared in an earlier question. Since text cannot currently be added to stackedplot axes (as for r2019b, let's hope that changes), here are two workarounds
Use an alternative function
(added on 11/19/20)
stackedaxes() on the file exchange is a limited alternative to Matlab's stackedplot and returns the axis handles so that you can add to or modify the plot after it is created.
Create your own custom stacked axes
Create axes, link their x-axis limits, and then add text to each axes.
% Create input table for demo because OP did not provide table
T = table((duration(0,0,17):seconds(1):duration(0,0,20))',...
{'abc','','bcd','cde'}',...
{'test1','','test3','test4'}',...
{'autoserv1','','autoserv3','autoserv4'}',...
'VariableNames',{'WeirdDuration','lot_test','Sublog','Message_test'})
T = 4x4 table
WeirdDuration lot_test Sublog Message_test _____________ __________ __________ _____________ 00:00:17 {'abc' } {'test1' } {'autoserv1'} 00:00:18 {0×0 char} {0×0 char} {0×0 char } 00:00:19 {'bcd' } {'test3' } {'autoserv3'} 00:00:20 {'cde' } {'test4' } {'autoserv4'}
% The only input would be "T", the table created above. It is assumed that
% The x values are stored in the first column named 'WeirdDuration' and that
% a subplot should be created for each subsequent column in T.
% Create figure with linked subplots stacked vertically
fig = figure();
nSub = size(T,2)-1; %number of subplots assuming 1 subplot for each col of T except the first
% in order to maximize spaces, create subplots manually.
margins = [.11 .11 .08 .12 .01]; %margins: left, right, bottom, top, vertical space between plots
height = (1-sum(margins(3:4)) - (nSub-1)*margins(5))/nSub; %height of each subplot
width = 1-sum(margins(1:2)); % width of each subplot
subPos = margins(3):height+margins(5):1; %vertical position of each subplot from bottom to top (may have extra values at end)
subHand = arrayfun(@(i)axes(fig,'Position',[margins(1),subPos(i),width,height]),1:nSub);
% add grids
set(subHand,'XGrid','on') % you could add 'YGrid','on' if you wish
% How loop through each subplot (i) and add text from column i+1
x = T.WeirdDuration; % the x coordinates of your text
y = 1:size(T,1); % the y coordinates of your text
for i = 1:nSub
th = text(subHand(i), x, y, T{:,i+1},'VerticalAlignment','Bottom');
ylim(subHand(i),[min(y),max(y)+1])
xlim(subHand(i),[min(x),max(x)+range(x)*.1]) %adds 10% of axis range
end
% Link x axes so the x limits are always the same
linkaxes(subHand,'x');
% Remove x tick for all but bottom axes
set(subHand(2:end),'XTickLabel', [])

5 comentarios

Life is Wonderful
Life is Wonderful el 3 de Dic. de 2019
Editada: Life is Wonderful el 19 de Nov. de 2020
Looks promising . I'II integrated and test the proposal.
Is it possible to move the table element along with plotted text data ? That would be wonderful.
Thank you!
Additional code can be found in the below link file. This was developed due to lack of stackedplot! You can suggest improvement as well.
Thanks a lot!!
I would request your help with your code under (PlotButton)
% Button pushed function: PlotButton
fig = uifigure;
uit = uitable(fig,'Data',f_table,...
you suggested changes was for figure (ReadFileButton)
% Button pushed function: ReadFileButton
function ReadFileButtonPushed(app, event)
  • My Full App Designer code
classdef Basic_Gui_autoreflow < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
GridLayout matlab.ui.container.GridLayout
LeftPanel matlab.ui.container.Panel
CenterPanel matlab.ui.container.Panel
UIAxes matlab.ui.control.UIAxes
ReadFileButton matlab.ui.control.Button
RightPanel matlab.ui.container.Panel
UITable matlab.ui.control.Table
PlotButton matlab.ui.control.Button
DropDown1 matlab.ui.control.DropDown
DropDown2 matlab.ui.control.DropDown
DropDown3 matlab.ui.control.DropDown
DropDown4 matlab.ui.control.DropDown
IndexToAnalyzeEditFieldLabel matlab.ui.control.Label
IndexToAnalyzeEditField matlab.ui.control.NumericEditField
SliderLabel matlab.ui.control.Label
Slider matlab.ui.control.Slider
end
% Properties that correspond to apps with auto-reflow
properties (Access = private)
onePanelWidth = 576;
twoPanelWidth = 768;
end
% Callbacks that handle component events
methods (Access = private)
% Changes arrangement of the app based on UIFigure width
function updateAppLayout(app, event)
currentFigureWidth = app.UIFigure.Position(3);
if(currentFigureWidth <= app.onePanelWidth)
% Change to a 3x1 grid
app.GridLayout.RowHeight = {606, 606, 606};
app.GridLayout.ColumnWidth = {'1x'};
app.CenterPanel.Layout.Row = 1;
app.CenterPanel.Layout.Column = 1;
app.LeftPanel.Layout.Row = 2;
app.LeftPanel.Layout.Column = 1;
app.RightPanel.Layout.Row = 3;
app.RightPanel.Layout.Column = 1;
elseif (currentFigureWidth > app.onePanelWidth && currentFigureWidth <= app.twoPanelWidth)
% Change to a 2x2 grid
app.GridLayout.RowHeight = {606, 606};
app.GridLayout.ColumnWidth = {'1x', '1x'};
app.CenterPanel.Layout.Row = 1;
app.CenterPanel.Layout.Column = [1,2];
app.LeftPanel.Layout.Row = 2;
app.LeftPanel.Layout.Column = 1;
app.RightPanel.Layout.Row = 2;
app.RightPanel.Layout.Column = 2;
else
% Change to a 1x3 grid
app.GridLayout.RowHeight = {'1x'};
app.GridLayout.ColumnWidth = {12, '1x', 434};
app.LeftPanel.Layout.Row = 1;
app.LeftPanel.Layout.Column = 1;
app.CenterPanel.Layout.Row = 1;
app.CenterPanel.Layout.Column = 2;
app.RightPanel.Layout.Row = 1;
app.RightPanel.Layout.Column = 3;
end
end
% Value changed function: Slider
function SliderValueChanged(app, event)
value = app.Slider.Value;
end
% Button pushed function: PlotButton
function PlotButtonPushed(app, event)
figHandles = findall(groot, 'Type', 'figure'); % Since version R2014b
figHandles = findall(0, 'Type', 'figure'); % Earlier versions
xstart = ceil(app.IndexToAnalyzeEditField.Value);
y = ceil(app.Slider.Value);
% plot data
x = xstart;
xend = size(app.UITable.Data,1);
% s_Time = timeseries(app.UITable.Data.WeirdDuration);
% s_Time = duration(app.UITable.Data.WeirdDuration);
s_DropDown1 = app.UITable.Data.(app.DropDown1.Value);
s_DropDown2 = app.UITable.Data.(app.DropDown2.Value);
s_DropDown3 = app.UITable.Data.(app.DropDown3.Value);
s_DropDown4 = app.UITable.Data.(app.DropDown4.Value);
c= app.UITable.Data;
mode = 'manual';
col = 'b-r-c-k-y-g-m-b-b-r-c-k-y-g-m-b-b-r-c-k-y-g-m-b';
% range =x;
f_table = app.UITable.Data(:,app.UITable.Data.Properties.VariableNames);
fig = uifigure;
uit = uitable(fig,'Data',f_table,...
'ColumnName',f_table.Properties.VariableNames,...
'RowName',f_table.Properties.RowNames,...
'ColumnWidth',{100}); %,"BackgroundColor",102,"BusyAction","queue");
uit.RowName = 'numbered';
uit.ColumnSortable = true;
uit.BackgroundColor = [1 1 .9; .9 .95 1;1 .5 .5];
uit.Data = f_table;
styleIndices = ismissing(f_table);
[row,col] = find(styleIndices);
s = uistyle('BackgroundColor','black');
addStyle(uit,s,'cell',[row,col]);
uit.DisplayDataChangedFcn = @updatePlot;
get(uit);
while x > 0
% range = (xstart:x); % Test combination
range = (x-5:1:x+5);
first = min(c{range,1});
last = max(c{range,1});
% U = duration([uit.Data{range,1}]);
% s = stackedplot(U, 'r-sq');
% text(U,range,s_DropDown1(range),range,'VerticalAlignment','bottom',"FontSize",9,"Color",col(mod(x,5)+1));
h = duration([c{range,1}]);
plot(app.UIAxes,h,range,"Color",rand(1,3));
hText1 = text(app.UIAxes,h,range,s_DropDown1(range),range,'VerticalAlignment','bottom',"FontSize",9.5,"Color",rand(1,3));
hold(app.UIAxes,'on'); grid on;
hText2 = text(app.UIAxes,h,range,s_DropDown2(range),range,'VerticalAlignment','bottom',"FontSize",10,"Color",rand(1,3));
hold(app.UIAxes,'on'); grid on;
hText3 = text(app.UIAxes,h,range,s_DropDown3(range),range,'VerticalAlignment','bottom',"FontSize",10.5,"Color",rand(1,3));
hold(app.UIAxes,'on'); grid on;
hText4 = text(app.UIAxes,h,range,s_DropDown4(range),range,'VerticalAlignment','bottom',"FontSize",11,"Color",rand(1,3));
hold(app.UIAxes,'on'); grid on;
el = addlistener(struct(app.UIAxes).Axes, 'XLim', 'PostSet', @(src, evnt)disp("XLim changed"));
el = addlistener(struct(app.UIAxes).Axes, 'YLim', 'PostSet', @(src, evnt)disp("YLim changed"));
delete(el);
if strcmp(mode, 'manual')
PlotCntrl = input('+: cont, -: back, a=: auto, q: quit -->', 's');
if strcmp(PlotCntrl, 'a')
mode = 'auto';
elseif strcmp(PlotCntrl, 'q')
x = 0;
elseif strcmp(PlotCntrl, '-')
if x>1, x=x-3; end
else
if x<xend-1, x=x+1; end
end
else
x = x+1;
if x>xend, x = xend; mode = 'manual'; end
end
pause(1.00);
drawnow;
end
end
% Button pushed function: ReadFileButton
function ReadFileButtonPushed(app, event)
[file,path] = uigetfile('*.mat', 'Select a File');
if isequal(file,0)
disp('User selected Cancel')
else
load(fullfile(path,file));
VariableNames=joinedtimetable.Properties.VariableNames;
app.DropDown1.Items = VariableNames;
app.DropDown2.Items = VariableNames;
app.DropDown3.Items = VariableNames;
app.DropDown4.Items = VariableNames;
x = app.IndexToAnalyzeEditField.Value;
y = app.Slider.Value;
% TableCellData= table2cell(timetable2table(joinedtimetable));
TableCellData1 = timetable2table(joinedtimetable);
app.UITable.Data = TableCellData1;
% app.UITable.Data = table2cell(timetable2table(joinedtimetable));
% app.UITable.Data.WeirdDuration
% app.UITable.ColumnName = app.UITable.Data(:,app.UITable.Data.Properties.VariableNames)
% app.UITable.ColumnEditable = true;
% app.UITable.ColumnName = VariableNames;
t_table = app.UITable.Data(:,app.UITable.Data.Properties.VariableNames);
app.UITable.ColumnName = t_table.Properties.VariableNames;
app.UITable.RowName = 'numbered';
app.UITable.ColumnSortable = true;
app.UITable.BackgroundColor = [1 1 .9; .9 .95 1;1 .5 .5];
get(app.UITable);
varData = whos;
saveIndex = cellfun(@isempty, regexp({varData.class}, 'matlab.(graphics|ui)'));
saveVars = {varData(saveIndex).name};
save('no_handles.mat', saveVars{:});
end
end
% Value changed function: DropDown1
function DropDown1ValueChanged(app, event)
value = app.DropDown1.Value;
end
% Value changed function: DropDown2
function DropDown2ValueChanged(app, event)
value = app.DropDown2.Value;
end
% Value changed function: DropDown3
function DropDown3ValueChanged(app, event)
value = app.DropDown3.Value;
% event.Source.BackgroundColor =[1 0 1];
% event.Source.FontColor = [0.8 0.8 0.8];
% event.Source.Visible = 'on';
end
% Value changed function: DropDown4
function DropDown4ValueChanged(app, event)
value = app.DropDown4.Value;
end
% Display data changed function: UITable
function UITableDisplayDataChanged(app, event)
DisplayData = app.UITable.DisplayData;
end
% Callback function: UITable, UITable
function UITableCellSelection(app, event)
indices = event.Indices;
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.IntegerHandle = 'on';
app.UIFigure.NumberTitle = 'on';
app.UIFigure.AutoResizeChildren = 'off';
app.UIFigure.Position = [100 100 1020 606];
app.UIFigure.Name = 'UI Figure';
app.UIFigure.SizeChangedFcn = createCallbackFcn(app, @updateAppLayout, true);
% Create GridLayout
app.GridLayout = uigridlayout(app.UIFigure);
app.GridLayout.ColumnWidth = {12, '1x', 434};
app.GridLayout.RowHeight = {'1x'};
app.GridLayout.ColumnSpacing = 0;
app.GridLayout.RowSpacing = 0;
app.GridLayout.Padding = [0 0 0 0];
app.GridLayout.Scrollable = 'on';
% Create LeftPanel
app.LeftPanel = uipanel(app.GridLayout);
app.LeftPanel.Layout.Row = 1;
app.LeftPanel.Layout.Column = 1;
% Create CenterPanel
app.CenterPanel = uipanel(app.GridLayout);
app.CenterPanel.BackgroundColor = [0 1 1];
app.CenterPanel.Layout.Row = 1;
app.CenterPanel.Layout.Column = 2;
% Create UIAxes
app.UIAxes = uiaxes(app.CenterPanel);
title(app.UIAxes, 'Time vs Messages')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
app.UIAxes.PlotBoxAspectRatio = [1.06988352745424 1 1];
app.UIAxes.BoxStyle = 'full';
app.UIAxes.XGrid = 'on';
app.UIAxes.YGrid = 'on';
app.UIAxes.Position = [6 15 564 540];
% Create ReadFileButton
app.ReadFileButton = uibutton(app.CenterPanel, 'push');
app.ReadFileButton.ButtonPushedFcn = createCallbackFcn(app, @ReadFileButtonPushed, true);
app.ReadFileButton.IconAlignment = 'center';
app.ReadFileButton.BackgroundColor = [0.9412 0.9412 0.9412];
app.ReadFileButton.FontName = 'Arial Black';
app.ReadFileButton.FontColor = [0.7176 0.2745 1];
app.ReadFileButton.Position = [241 569 100 26];
app.ReadFileButton.Text = 'Read File';
% Create RightPanel
app.RightPanel = uipanel(app.GridLayout);
app.RightPanel.ForegroundColor = [0.149 0.149 0.149];
app.RightPanel.TitlePosition = 'centertop';
app.RightPanel.Title = 'Configuration_parameters';
app.RightPanel.BackgroundColor = [0.0588 1 1];
app.RightPanel.Layout.Row = 1;
app.RightPanel.Layout.Column = 3;
app.RightPanel.FontName = 'Arial Black';
app.RightPanel.FontWeight = 'bold';
% Create UITable
app.UITable = uitable(app.RightPanel);
app.UITable.ColumnName = {'Column 1'; 'Column 2'; 'Column 3'; 'Column 4'};
app.UITable.RowName = {};
app.UITable.CellEditCallback = createCallbackFcn(app, @UITableCellSelection, true);
app.UITable.CellSelectionCallback = createCallbackFcn(app, @UITableCellSelection, true);
app.UITable.DisplayDataChangedFcn = createCallbackFcn(app, @UITableDisplayDataChanged, true);
app.UITable.Position = [6 52 422 260];
% Create PlotButton
app.PlotButton = uibutton(app.RightPanel, 'push');
app.PlotButton.ButtonPushedFcn = createCallbackFcn(app, @PlotButtonPushed, true);
app.PlotButton.Position = [121 530 100 22];
app.PlotButton.Text = 'Plot';
% Create DropDown1
app.DropDown1 = uidropdown(app.RightPanel);
app.DropDown1.ValueChangedFcn = createCallbackFcn(app, @DropDown1ValueChanged, true);
app.DropDown1.FontColor = [0.4941 0.1843 0.5569];
app.DropDown1.Position = [241 490 67.9753521126761 22];
% Create DropDown2
app.DropDown2 = uidropdown(app.RightPanel);
app.DropDown2.ValueChangedFcn = createCallbackFcn(app, @DropDown2ValueChanged, true);
app.DropDown2.FontColor = [0.4941 0.1843 0.5569];
app.DropDown2.Position = [241 430 67.9753521126761 22];
% Create DropDown3
app.DropDown3 = uidropdown(app.RightPanel);
app.DropDown3.ValueChangedFcn = createCallbackFcn(app, @DropDown3ValueChanged, true);
app.DropDown3.FontColor = [0.4941 0.1843 0.5569];
app.DropDown3.Position = [241 370 67.9753521126761 22];
% Create DropDown4
app.DropDown4 = uidropdown(app.RightPanel);
app.DropDown4.ValueChangedFcn = createCallbackFcn(app, @DropDown4ValueChanged, true);
app.DropDown4.FontColor = [0.4941 0.1843 0.5569];
app.DropDown4.Position = [241 320 67.9753521126761 22];
% Create IndexToAnalyzeEditFieldLabel
app.IndexToAnalyzeEditFieldLabel = uilabel(app.RightPanel);
app.IndexToAnalyzeEditFieldLabel.BackgroundColor = [1 1 1];
app.IndexToAnalyzeEditFieldLabel.HorizontalAlignment = 'right';
app.IndexToAnalyzeEditFieldLabel.FontColor = [0 0 1];
app.IndexToAnalyzeEditFieldLabel.Position = [21 480 90 22];
app.IndexToAnalyzeEditFieldLabel.Text = 'IndexToAnalyze';
% Create IndexToAnalyzeEditField
app.IndexToAnalyzeEditField = uieditfield(app.RightPanel, 'numeric');
app.IndexToAnalyzeEditField.FontColor = [0 0 1];
app.IndexToAnalyzeEditField.Position = [118 480 100 22];
app.IndexToAnalyzeEditField.Value = 25;
% Create SliderLabel
app.SliderLabel = uilabel(app.RightPanel);
app.SliderLabel.HorizontalAlignment = 'center';
app.SliderLabel.FontColor = [0 0 1];
app.SliderLabel.Position = [22 420 36 22];
app.SliderLabel.Text = 'Slider';
% Create Slider
app.Slider = uislider(app.RightPanel);
app.Slider.Limits = [1 10];
app.Slider.MajorTicks = [0 10 20 40 60 80 100 101 102 103 104];
app.Slider.ValueChangedFcn = createCallbackFcn(app, @SliderValueChanged, true);
app.Slider.FontColor = [0 0 1];
app.Slider.Position = [27 453 182 3];
app.Slider.Value = 5;
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = Basic_Gui_autoreflow
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
Adam Danz
Adam Danz el 3 de Dic. de 2019
"I would request your help with your code under (PlotButton)"
Sure's what's your question?
Adam Danz
Adam Danz el 4 de Dic. de 2019
Editada: Adam Danz el 5 de Dic. de 2019
This topic is a lot different from your original question. Has your original question been answered?
Regarding the scroll bar, it looks like your UITable already has a vertical and horizontal scroll bar so I'm not sure what you're looking for.
Life is Wonderful
Life is Wonderful el 5 de Dic. de 2019
I have raised new thread. Can you please look into it
Thank you!!

Iniciar sesión para comentar.

Más respuestas (1)

Duncan Po
Duncan Po el 2 de Dic. de 2019

0 votos

What do you expect to see with your text data in the plot?
stackedplot ignores text data, but plots categorical data. If your text data can be converted into categorical (contains only a relatively small number of choices), you should convert and then plot. There is a convertvars function that can do the conversion for you.

1 comentario

Life is Wonderful
Life is Wonderful el 19 de Abr. de 2021
@Adam Danz, Hi Adam,
I having facing difficulties for generating the twiddle factor lookUp table using cordic sine and cordic cosine
Thank you!

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Object Properties en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 29 de Nov. de 2019

Comentada:

el 19 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by