Effacer les filtres
Effacer les filtres

why cant I show the source file name on several graphs?

2 vues (au cours des 30 derniers jours)
sason yaakov bochnick
sason yaakov bochnick le 31 Août 2023
I was able to show the name of the source file on a graph when it is a single graph. But when I tried the process on a number of files and graphs, it wrote to me that it was unable to display the information
% Reading several data files:
data_path = 'C:\Users\sason\safty\abcd';
matfiles = dir(fullfile(data_path, '*.txt'));
nfiles = length(matfiles);
data = cell(nfiles);
%%
fileNames = cell(nfiles , 1);
for i = 1:nfiles
fileNames{i} = matfiles(i).name;
end
%%
for i = 1 : nfiles
full_data = readtable(matfiles(i).name,'NumHeaderLines',2);
voltage_1 = full_data.Var2(end-990:end);
voltage_2 = full_data.Var3(end-990:end);
time = full_data.Var1(end-990:end);
v1_func_time(:,:,i) = voltage_1;
v2_func_time(:,:,i) = voltage_2;
end
%%
for j = 1 : nfiles
title('voltage_1(time)');
xlabel('Time (second)');
ylabel('Voltage_1(volts)');
plot(time,v1_func_time(:,:,j));
% Get the current axes handle
ax = gca;
% Enable data cursors
datacursormode on;
% Get the data cursor object
dcm_obj = datacursormode(gcf);
% Set the update function
set(dcm_obj, 'UpdateFcn', {@displayFilename, fileNames(i)});
hold on;
end
%%
function txt = displayFilename(~, event_obj, filename)
pos = get(event_obj, 'Position');
txt = {['X: ', num2str(pos(1))], ['Y: ', num2str(pos(2))], ['File: ', filename]};
end
Can you help me solve the problem? And if you see additional comments in general about the code, I'd love for you to tell me.
thenks!

Réponses (1)

dim-ask
dim-ask le 31 Août 2023
Here is a small working example that plots some lines and puts some text to display when you click on.
f = figure;
names = {'plot1' 'plot2' 'plot3'}; % you can replace this with your filenames
hold on
for iLine = 1:3
plot(1:4,(1:4).^iLine,'Tag',names{iLine}); % plot your lines, and add a tag in each
end
datacursormode on;
% Get the data cursor object
dcm_obj = datacursormode(f);
% Set the update function
set(dcm_obj, 'UpdateFcn', @displayFilename); % notice this is applied to the whole plot, not each line
function txt = displayFilename(~, event_obj)
pos = get(event_obj, 'Position');
txt = {['X: ', num2str(pos(1))], ['Y: ', num2str(pos(2))], ['File: ', event_obj.Target.Tag]};
end
note that dcm_obj applies to the whole plot window (notice that you call it with the argument gcf which is basically the whole window; actually gcf is the figure f you created in the beginning), not just each line separately; so when you call it on each loop, it just overwrites the previous one.
Instead, you can add a Tag on each line you plot (eg add 'Tag',fileNames{i} inside the plot function call) and use that to add the text in displayFilename. And then you need to call set(dcm_obj, 'UpdateFcn', @displayFilename); only once in the end of your loop. Similar with hold on, you may only have to call it once in the beginning not on each loop (just to make things more clear).
  1 commentaire
sason yaakov bochnick
sason yaakov bochnick le 7 Sep 2023
hey and thanks for your answer!
I need to add the file source of the graph on a plot with 1000 graph (all the 1000 graph need to be on one plot). Therefor, i cant use your method with list of the plot. i tried to use a for loop that take the i graph and tag it with the name of the file that created it. For some reason the tag take the final file only for all the plots.
(**note: in my code now theres a few graph on the plot only, because its a test)
Thank you!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by