# Matlab - reach data from another column based on point selected on graph

조회 수: 6(최근 30일)
Michal Karwacki 2021년 12월 27일
답변: Pranjal Kaura 2021년 12월 29일
Hi!
I have created a program in MATLAB that reads values from a table (*csv) and creates graphs based on that. The table from the file contains 18 columns with values with different types of variables. I used the values from the X column and the Y column to create the graph.
I would like to get the value from another column of the row based on the selected point of the chart.
For example (image below):
When I selected on the indicated point, the values from the table appeared:
X value: -227334
Y value: -39060.2
After manually checking, I can read that the row with these values has a value of "1" assigned in the "Route" column. I would like to automate the search of this table and automatically print the value of the "Route" column for the indicated row.
How could I do that?
Best Regards,
Michael

댓글을 달려면 로그인하십시오.

### 답변(1개)

Pranjal Kaura 2021년 12월 29일
Hey Michal,
It's my understanding that you want to interact with your plot, get certain values of coordinates (x, y) from the figure by clicking on a particular point and then do further computations based on the values obtained.
You can go through Enable data cursor mode - MATLAB (mathworks.com) documentation to do the same. Here's a code snippet you can refer to:
x = linspace(0, 7, 100);
y = linspace(0, 3, 100);
curve1 = x/6;
curve2 = ones(100, 1)*0.5;
curve3 = ones(100, 1);
f = figure;
plot(x, curve1, 'r', 'LineWidth', 2);
hold on;
plot(x, curve3, 'b', 'LineWidth', 2);
plot(curve2, y, 'k', 'LineWidth', 2);
dcm = datacursormode(f);
dcm.Enable = 'on';
dcm.UpdateFcn = @updateCoordinates;
function txt = updateCoordinates(~,info)
x = info.Position(1);
y = info.Position(2);
disp([x y]); % you can use the coordinates here to index into your table
% and find the corresponding row index
txt = ['(' num2str(x) ', ' num2str(y) ')'];
end
Now that you have the 'x' and 'y' coordinates, you can refer the following code snippet to find the corresponding row in your table having these values.
table = [1 2 3; 4 5 6; 7 8 9];
[row1 col1] = find(table == 7);
[row2 col2] = find(table == 9);
intersect(row1, row2)
Hope this helps!

댓글을 달려면 로그인하십시오.

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by