Visibility of several plot in the same axes by checkmark in GUI

1 回表示 (過去 30 日間)
JB
JB 2017 年 9 月 27 日
コメント済み: JB 2017 年 9 月 27 日
I'm preparing a gui with 2-30 plots where the visibility is controlled by individual checkmark selections. The code is working perfectly when I show two plots in different axes (see gui below)
But if I want to show the two plots in the same axes (axes1) I get this error:
Error using matlab.graphics.chart.primitive.Line/set
Invalid or deleted object.
Error in test>checkbox1_Callback (line 93)
set(handle_plot1, 'visible' , 'on')
What am I doing wrong?
Here is my code:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
X1=[1 2 3 4];
Y1=[-1 -2 -3 -4];
X2=[1 2 3 4];
Y2=[1 2 3 4];
set(gca, 'NextPlot', 'add'); % Equivalent to: hold('on')
handles.handle_plot1 = plot(X1,Y1,'visible','off','LineWidth',2,'color', [0 0 0],'linestyle', '--','parent',handles.axes1);
handles.handle_plot2 = plot(X2,Y2,'visible','off','LineWidth',2,'color', [1 0 0],'linestyle', '--','parent',handles.axes1);
guidata(hObject, handles);
% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
handle_plot1 = handles.handle_plot1
if (get(hObject,'Value')) == 1
set(handle_plot1, 'visible' , 'on')
else
set(handle_plot1, 'visible' , 'off')
end
% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
handle_plot2 = handles.handle_plot2
if (get(hObject,'Value')) == 1
set(handle_plot2, 'visible' , 'on')
else
set(handle_plot2, 'visible' , 'off')

採用された回答

Jan
Jan 2017 年 9 月 27 日
編集済み: Jan 2017 年 9 月 27 日
Now try:
set(handles.axes1, 'NextPlot', 'add'); % Not "gca" as in my last suggestion
handles.handle_plot1 = plot(X1,Y1,'visible','off','LineWidth',2, ...
'color', [0 0 0],'linestyle', '--', 'parent', handles.axes1);
handles.handle_plot2 = plot(X2,Y2,'visible','off','LineWidth',2, ...
'color', [1 0 0],'linestyle', '--', 'parent', handles.axes1);
guidata(hObject, handles);
By the way: I use a specific function to create the 'on', 'off' dynamically:
function S = OnOffStr(D)
OffOn = {'off', 'on'};
L = (D ~= 0) + 1; % 0/FALSE => 1, anything else => 2
if length(L) == 1
S = OffOn{L}; % Reply a string
else
S = OffOn(L); % Reply a cell string
end
Then the callback looks like:
function checkbox1_Callback(hObject, eventdata, handles)
set(handles.handle_plot1, 'Visible', OnOffStr(get(hObject,'Value')));
  1 件のコメント
JB
JB 2017 年 9 月 27 日
That is brilliant. Thanks a lot Jan Simon.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeClassical Control Design についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by