how can I link subplots (for brush data)?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
how can I link subplots (for brush data)?
this is my code :
colors = {'r', 'g', 'b', 'c', 'm', 'y', 'k', [0, 0.5, 0.5], [0.5, 0.5, 0], [0, 0.5, 0], [0.5, 0, 0],[0, 0, 0.5], [0.5, 0.5, 0.5],[0.3010 0.7450 0.9330],[0.4660 0.6740 0.1880],[0.4940 0.1840 0.5560],[0.9290 0.6940 0.1250],[0.8500 0.3250 0.0980],[0.6350, 0.0780, 0.1840],[0 0.4470 0.7410] };
[filename,pathname] = uigetfile('*.csv', 'Select CSV files', 'MultiSelect', 'on');
if isnumeric(filename) % user clicked Cancel
return
end
s=size(filename,2);
for i=1:s
o= readtable(fullfile(pathname,filename{i}));
cData1=table2array(o(:,1));
cData2=table2array(o(:,2));
fig = figure();
subplot(3,1,1)
p1=plot( cData1,cData2,...
'Color', colors{mod(i, numel(colors)) + 1});
hold('on')
end
for i=1:s
o= readtable(fullfile(pathname,filename{i}));
cData1=table2array(o(:,1));
cData3=table2array(o(:,3));
subplot(3,1,2)
p2=plot( cData1,cData3,...
'Color', colors{mod(i, numel(colors)) + 1});
% grid(app.UIAxes2_2,'on')
hold('on')
end
%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:s
o= readtable(fullfile(pathname,filename{i}));
cData1=table2array(o(:,1));
cData4=table2array(o(:,4));
subplot(3,1,3)
p3=plot( cData1,cData4,...
'Color', colors{mod(i, numel(colors)) + 1});
% grid(app.UIAxes2,'on')
hold('on')
end
b=brush(fig);
b.ActionPostCallback = {@onBrushAction,p1,p2,p3};
% callback function
function onBrushAction(~, eventdata,p1,p2,p3)
set(p1, 'BrushData', eventdata.Axes.Children.BrushData)
set(p2, 'BrushData', eventdata.Axes.Children.BrushData)
set(p3, 'BrushData', eventdata.Axes.Children.BrushData)
end
4 Kommentare
Antworten (2)
NVSL
am 27 Jan. 2025
Bearbeitet: Walter Roberson
am 3 Feb. 2025
I have gone through your code and executed it using the CSV files you provided in response to Mathieu's comment. Assuming you want a 3x1 subplot where each plot is a line graph of each file’s 1st column with 2nd, 3rdand 4th columns respectively.
In the initial loop, you have “fig = figure()” executing for each file. This prevents first subplot from displaying super imposed line plots despite using “hold(on)” as “figure()” creates a new figure each time it’s called. To fix this issue I suggest moving the line “fig = figure()” above the 1st for loop. The below code snippet demonstrates the required modification.
s=size(filename,2);
%Create a single figure
fig = figure();
%.....
for i=1:s
o= readtable(fullfile(pathname,filename{i}));
cData1=table2array(o(:,1));
cData2=table2array(o(:,2));
%fig = figure();
For more details please refer to the following MathWorks documentation:
Hope this helps!
Mathieu NOE
am 3 Feb. 2025
Bearbeitet: Mathieu NOE
am 3 Feb. 2025
I can add these little upgrades to your code , like having only one call to readtable at each loop iteration and other minor stuff, so at the end it's more compact, readable and better looking
here the x axes linking seems to work along the brush without conflict
colors = {'r', 'g', 'b', 'c', 'm', 'y', 'k', [0, 0.5, 0.5], [0.5, 0.5, 0], [0, 0.5, 0], [0.5, 0, 0],[0, 0, 0.5], [0.5, 0.5, 0.5],[0.3010 0.7450 0.9330],[0.4660 0.6740 0.1880],[0.4940 0.1840 0.5560],[0.9290 0.6940 0.1250],[0.8500 0.3250 0.0980],[0.6350, 0.0780, 0.1840],[0 0.4470 0.7410] };
[filename,pathname] = uigetfile('*.csv', 'Select CSV files', 'MultiSelect', 'on');
if isnumeric(filename) % user clicked Cancel
return
end
s=size(filename,2);
fig = figure();
hold('on')
for i=1:s
o= readtable(fullfile(pathname,filename{i}));
subplot(3,1,1)
cData1=table2array(o(:,1));
cData2=table2array(o(:,2));
p1=plot( cData1,cData2,'Color', colors{mod(i, numel(colors)) + 1});
subplot(3,1,2)
cData3=table2array(o(:,3));
p2=plot( cData1,cData3,'Color', colors{mod(i, numel(colors)) + 1});
subplot(3,1,3)
cData4=table2array(o(:,4));
p3=plot( cData1,cData4,'Color', colors{mod(i, numel(colors)) + 1});
end
% Link the x-axes of all subplots
linkaxes(findall(gcf, 'Type', 'axes'), 'x');
% brush
b=brush(fig);
b.ActionPostCallback = {@onBrushAction,p1,p2,p3};
% callback function
function onBrushAction(~, eventdata,p1,p2,p3)
set(p1, 'BrushData', eventdata.Axes.Children.BrushData)
set(p2, 'BrushData', eventdata.Axes.Children.BrushData)
set(p3, 'BrushData', eventdata.Axes.Children.BrushData)
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Subplots 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!