Filter löschen
Filter löschen

Identify lines in a plot by clicking on them

12 Ansichten (letzte 30 Tage)
Gino Sartori
Gino Sartori am 26 Jan. 2024
Kommentiert: Gino Sartori am 9 Feb. 2024
I have to import a bunch of .txt files (approx. 200) from a folder and create a line plot of the first vs. third column.
my code is:
input_folder = uigetdir('start_path','dialog_title');
files = dir(fullfile(input_folder, '*.txt'));
file_paths = fullfile({files.folder}, {});
for i = 1 : numel(file_paths)
data = readmatrix(file_paths{i},'Delimiter',{',','='});
hold on
Now I would like to be able to identify (i.e. get the corresponding file name) the lines in the plot by clicking on them. Any ideas on how to achieve such a result?
  3 Kommentare
Gino Sartori
Gino Sartori am 26 Jan. 2024
I have tried to add a line of text to datatip using the dataTipTextRow function (based on this example) but cannot achieve what I want. I assume the problem comes from the fact that I loop through the .txt files containing the data.
Walter Roberson
Walter Roberson am 26 Jan. 2024
It would help if you were to
[~,partname] = fileparts(file_paths{i});
plot(data(:,1), data(:,3), '-r', 'displayname', partname);

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Pavan Sahith
Pavan Sahith am 6 Feb. 2024
Hello Gino,
I've noticed that you're working with multiple .txt files to create line plots and you're interested in finding a way to identify which line corresponds to which file by simply clicking on it.
If you want to identify lines in the plot by clicking on them, you can try using the UserData property of the plot object and a custom data cursor update function.
I've put together a code example that will generate a plot with lines for each of your text files. When you interact with a line on this plot, a data cursor will pop up showing the X and Y coordinates of the selected point, as well as the name of the related file.
This method uses an update function for the "data cursor mode" that fetches the file's name from the "UserData" property attached to each plotted line.
Please refer to the following MathWorks documentation to know more about
Hope this helps

Weitere Antworten (0)


Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by