Plots in one graph GUI evalin
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello! I have a listbox and I would like that when I select several inputs in the listbox and click a button called "plot", the axes plots the variables I have selected. The variables could be "var1,2,3,4" but not var_p because it's fixed, it's the time. This is my code:
function [var_p,var1_p,var2_p,var3_p,var4_p] = get_var_names1(handles)
% Returns the names of the variables to plot_button1
global index_selected
list_entries = get(handles.listbox1,'String');
index_selected = get(handles.listbox1,'Value');
var_p = 'Temps';
var1_p =[];
var2_p =[];
var3_p =[];
var4_p =[];
if isempty(index_selected)
errordlg('You must select one variable','Incorrect Selection','modal')
else
%var = list_entries{index_selected(1)};
var1_p = list_entries{index_selected(1)};
var2_p = list_entries{index_selected(2)};
var3_p = list_entries{index_selected(3)};
var4_p = list_entries{index_selected(4)};
end
function plot_button1_Callback(hObject, eventdata, handles)
global index_selected
[x,y1,y2,y3,y4] = get_var_names1(handles);
if isempty(x) && isempty(y1)
return
end
axes(handles.axes1);
figure(gcf)
try
if index_selected==1 && index_selected==2 && index_selected==3 && index_selected==4
evalin('base',['plot(',x,',',y1,',',x,',',y2,',',x,',',y3,',',x,',',y4,')'])
datetick('x', 31, 'keepticks','keeplimits' ); %'keepticks' 'keeplimits'
xlabel('Date')
xticklabel_rotate; % calls file (available from matlab file exchange) that allows to display the label's ticks with a rotation of 45°
catch ex
errordlg(...
ex.getReport('basic'),'Error generating linear plot','modal')
end
This code kinda works but it means I have to do all the combinaisons like:
if index_selected==1
evalin('base',['plot(',x,',',y1,')]
if index_selected==1 && index_selected==2
evalin('base',['plot(',x,',',y1,',',x,',',y2,')])
ect... i have tried:
if index_selected==1
evalin('base',['plot(',x,',',y1,')'])
elseif index_selected==2
hold(handles.axes1)
evalin('base',['plot(',x,',',y2,')'])
elseif index_selected==3
evalin('base',['plot(',x,',',y3,')'])
elseif index_selected==4
evalin('base',['plot(',x,',',y4,')'])
hold off
end
but it doesnt work, gives me the error: Attempted to access index_selected(2); index out of bounds because numel(index_selected)=1.
Error in desk>get_var_names1 (line 348) var2_p = list_entries{index_selected(2)};
Error in desk>plot_button1_Callback (line 355) [x,y1,y2,y3,y4] = get_var_names1(handles);
I'm using R2013a. Can anyone help? Thank you!
댓글 수: 0
채택된 답변
Dimitris Iliou
2017년 6월 15일
If understand your question correctly, you are trying to plot the choices that are selected in a list-box.
The error that you are getting:
Attempted to access index_selected(2); index out of bounds because numel(index_selected)=1.
is because the length(index_selected) will be equal to the number of choices you have picked on the list-box. For example, if you only choose choices 3,4 the index_selected should be:
index_selected =
3 4
length(index_selected) =
2
and that is why you get the error.
So instead of using the code:
if isempty(index_selected)
errordlg('You must select one variable','Incorrect Selection','modal')
else
%var = list_entries{index_selected(1)};
var1_p = list_entries{index_selected(1)};
var2_p = list_entries{index_selected(2)};
var3_p = list_entries{index_selected(3)};
var4_p = list_entries{index_selected(4)};
end
You can modify it as:
if isempty(index_selected)
errordlg('You must select one variable','Incorrect Selection','modal')
else
for i=1:length(index_selected)
var_p = [var_p list_entries{index_selected(i)}];
end
end
The
var_p = [var_p list_entries{index_selected(i)}];
takes advantage of the fact that if you have a 5x3 matrix A, and you plot(A), then the figure will have 3 lines, each having 5 points.
You can verify that on your own by running the following script:
C = rand(5,1);
plot(C);
hold on
for i=1:3
D = rand(5,1);
C = [C D];
pause(1)
plot(C)
end
In every iteration, a new column is concatenated to the matrix (in your case it would be a different list-box choice).
So, instead of using eval and try to identify the indices, I would try using this notation to create a matrix that contains all the current choices, and then plot that matrix.
댓글 수: 0
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!