Make a legend appear depending on selctions from a uitable
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have a uitable where a figure is displayed and depending on the cells clicked, different plots appear on the figure or get deleted from it. I want to add a legend to this, that also changes depending on the choices made to reflect what is on the graph. The code is as follows, with my attempted solution.
function createMyTable2
f = figure('Position',[100 100 900 500],'Visible','on'); %#ok<NASGU>
ax = axes('Drawmode','Fast','Units','pixels'); %#ok<NASGU>
hold on
grid;
tableData = {'Sin(x)', false; 'Cos(x)', false; 'y = x', false};
columnNames = {'Function', 'Show'};
t = uitable('Units','normalized','Position',...
[0.8 0.8 0.2 0.2], 'Data', tableData,...
'ColumnName', columnNames, ...
'ColumnEditable', true,...
'CellEditCallback', @onCellSelected2);
plotHandles = zeros(3,1); % This first number needs to change depending on how many graphs are to be plotted
function onCellSelected2(hObject, data) %#ok<*INUSL>
fprintf('selecting cell (%d,%d)\n', data.Indices(1), data.Indices(2));
col = data.Indices(2);
if col == 2 % if the column of checkboxes is selected
row = data.Indices(1); % get the row
myData = get(t,'Data'); % get the uitable data
isChecked = myData{row, col}; % is the checkbox checked (true) or not (false)
if isChecked
if row == 1
x = 0:pi/6:2*pi;
y = sin(x);
hold on
plotHandles(row) = plot(x,y,'--','color','b'); % plot the function
legend('show','sin(x)','location','southwest')
elseif row == 2
u = 0:pi/6:2*pi;
p = cos(u);
plotHandles(row) = plot(u,p,'--','color','m'); % plot the function
legend('show','cos(x)','location','southwest')
elseif row == 3
j = 0:1:6;
k = j;
plotHandles(row) = plot(j,k,'--','color','k'); % plot the function
elseif row == 2 && row == 3
plotHandles(row) = plot(x,y,'--','color','b',u,p,'--','color','m'); % plot the function
legend('show','sin(x)','cos(x)','location','southeast') % This line does nothing
end
elseif ishandle(plotHandles(row))
delete(plotHandles(row));
delete(legend);
plotHandles(row) = 0; % delete the drawn function
end
end
end
end
1 Commento
Nirav Sharda
il 15 Giu 2017
Modificato: Nirav Sharda
il 15 Giu 2017
Instead on looping for values in the row variable, you can loop on the Data property of the table which is a cell-array.
>> t.Data
This would give you a cell-array and the second column has the values if the check-boxes are ticked or not. I hope this helps.
Risposte (0)
Vedere anche
Categorie
Scopri di più su Specifying Target for Graphics Output in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!