Plotting Selected Items from a List

I have been trying to figure out how I can plot one or more items that the user chooses from a list. I have 22 items in total that each have their own specific plots. Is there any way I can do this? So far, I have used listdlg() and if-else statements, but that only works if I only want to plot one item.

6 Comments

autumn - please post some of your code so that we can get an idea of what you have attempted. How do you plot the list items? Do you allow for multi-selection of list items?
[objects] = listdlg('ListString',... % my 22 objects are inserted here
'PromptString','Select an item:',...
'SelectionMode','multiple',...
'OKString','Plot',...
'CancelString','Exit');
if objects == 1
plot3(%coordinates)
elseif objects == 2
plot3(%coordinates)
% etc. up until
elseif objects == 22
plot3(%coordinates)
end
% This code plots one object at a time just fine, but when I
% try to allow for selection of more than one object, it no
% longer works. I didn't disclose coordinates or objects
% for academic reasons.
How are you getting the coordinates from the items that have been selected? Presumably objects is an array of indices. Also, please clarify what you mean by when I try to allow for selection of more than one object, it no longer works. What happens? Is there an error or something else?
I'm pulling the coordinates (x,y,z) from a 1x22 structure. I don't get an 'error' in my code if I attempt to plot multiple things, but nothing is plotted. If I select one object from my list, I can still plot it. I hope that makes more sense.
And [objects] is a 1x2 double, defined as [1,8] by my workspace.
Consider using the any function:
any(objects == 1)
Since objects is a 1x2, does that mean you have selected the first and eigth item in the list box? How are you then extracting the coordinates from the structure? Please show the code to do this.

Sign in to comment.

 Accepted Answer

Geoff Hayes
Geoff Hayes on 5 Jun 2019
Edited: Geoff Hayes on 5 Jun 2019
autumn - try the following
[objects] = listdlg('ListString',... % my 22 objects are inserted here
'PromptString','Select an item:',...
'SelectionMode','multiple',...
'OKString','Plot',...
'CancelString','Exit');
for k = 1:length(objects)
index = objects(k);
% get the coordinates for this object
plot3(coordinates);
hold on;
end
The above code will iterate over each selection (index) that you have selected in the list. The coordinates for that selection will be drawn and then, using hold on be retained when you try to draw the next selection.
Or you could combine your coordinates (from each selected object) in such a way that you just call plot3 once with the combined coordinates for the X, Y, and Z. See plot3 for details.

6 Comments

Geoff, how exactly would I call plot3 once when each object has its own set of coordinates? Would that be the syntax under the 'Plot Multiple Lines' of plot3's documentation page? If so, I'm not exactly sure of how to do that.
autumn - yes, the Plot Multiple Lines Using Matrices is what you could use...it will depend upon what you are doing now for the single plot case. You haven't shown that code beyond
plot3(%coordinates)
so I'm not sure exactly what you are putting as parameters to this function.
Alright, plotting those points as matrices worked, but now my code plots all 22 objects at once instead of what I select from the list.
for the single plot case:
(each object has an 'identity' and a 'position,' both found in the same structure)
[objects] = listdlg('ListString',... % creates list
% strings for objects are listed here,
% ex. 'pie' corresponds to object == 1,
% etc.
'PromptString','Select an object:',...
'SelectionMode','multiple',...
'OKString','Plot',...
'CancelString','Exit');
if object == 1
plot3(id(1).pos(x),id(1).pos(y),id(1).pos(z))
end
also, why is index written in your code above? is it doing anything?
in this code
for k = 1:length(objects)
index = objects(k);
% get the coordinates for this object
plot3(coordinates);
hold on;
end
objects is an array of one or more indices depending upon the items you have selected from your list. The length(objects) tells us the number of items that you have selected. We iterate over each one and obtain the index for each. Your code will then do something with that index, accessing something from your struct. If I use your code as a guide, then this would become
for k = 1:length(objects)
index = objects(k);
% get the coordinates for this object
plot3(id(index).pos(x),id(index).pos(y),id(index).pos(z))
hold on;
end
I'm not sure what your code means by pos(x) or pos(y) etc. Presumably that extracts the x and y and z coordinates (I just don't know the data type for these variables - integers?).
autumn
autumn on 5 Jun 2019
Edited: autumn on 5 Jun 2019
I got everything to work! Thank you so much for all of your help!
You're welcome! :)

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!