why cant I show the source file name on several graphs?
1 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(1 个)
dim-ask
2023-8-31
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).
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graph and Network Algorithms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!