필터 지우기
필터 지우기

Make a legend appear depending on selctions from a uitable

조회 수: 5 (최근 30일)
Kyle Reagan
Kyle Reagan 2017년 6월 8일
편집: Nirav Sharda 2017년 6월 15일
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
Nirav Sharda
Nirav Sharda 2017년 6월 15일
편집: Nirav Sharda 2017년 6월 15일
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.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by