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.