Domanda

I developed a Matlab GUI that plots data from a table to a histogram.

I want to be able to click on a bin and pull the rows from the table corresponding to the data in that bin only. When using data cursor, clicking on a bin will show you the bin center and edges as well as the bin count. I assume there must be a way to return the actual data grouped in those bins.

I've looked into ginput but I'm not sure if that's the right track or how I could use that to my benefit.

Any advice or guidance is appreciated!

Sample table:

'Col1'    'Col2'    'Col3'
Name1       1       Thing1
Name2       5       Thing2
Name3       2       Thing3
Name4       1       Thing4
Name5       8       Thing5
Name6       10      Thing6

Sample Code:

data = table.Col2;
hist(data); %histogram defaults to 10 bins

I wish to be able to actively click on a bin in the histogram and have the corresponding rows stored into another table.

So with the data above, the first bin in the histogram contains rows 1 and 4 from the table - once I click on that bin I want to see this stored in another table:

'Col1'    'Col2'    'Col3'
Name1       1       Thing1
Name4       1       Thing4

The same should happen if I clicked on any of the other bins, their respective data should be stored into tables.

The actual data is a few hundred rows long and multiple other columns. The data in Col2 are all numeric though not necessarily integers.

È stato utile?

Soluzione

You can set figure's ButtonDownFcn property to a function that will get the CurrentPoint with X coordinates.

set(gcf,'ButtonDownFcn',@your_bd_function)

(You can use any figure handle instead of gcf, of course.)

Then in the function

curpnt = get(gca,'CurrentPoint'); % returns [x y] vector where you clicked

(Again use appropriate axis handle here.)

If you have one value per bin and it's integer, then the rest is easy:

out = atable(data==round(curpnt(1)),:);

For more complex case you need to find the boundaries for the bins and select the data accordingly. It would be easier if you return the data from hist as [y,x] = hist(data,bin);

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top