Create a loop that add data from different datasets into a plot

7 views (last 30 days)
Hello,
I am trying to create a specific a loop that would allow me to add new data in a plot.
Let me explain, I have several datasets (12) with 3 ≠ sheets everytime from which I am calculating mean between rows, diff between rows and mean of differences.
I'd like to do that for all my data sets and then plot all the mean and diff on the same plot, without having to create a new dataset and save means and diffs independantly.
My aim is to create a bland-altman plot of every datasets based on Pre, BH and Post using different symbols between trials and colors between Pre,BH and Post.
Here is the row code doing so for one dataset:
for n=4 %first data set
file=[datapath,d(n).name];
Pre = readtable(file,'Sheet',"Pre"); %sheet 1 of dataset1
Pre=table2array(Pre(:,["Var3","Var8"]));
BH = readtable(file,'Sheet',"BH"); %sheet 2 of dataset1
BH=table2array(BH(:,["Var3","Var8"]));
Post = readtable(file,'Sheet',"Post"); %sheet 3 of dataset1
Post=table2array(Post(:,["Var3","Var8"]));
meanPre=mean(Pre,2);
meanBH=mean(BH,2);
meanPost=mean(Post,2);
diffPre=diff(Pre,1,2);
diffBH=diff(BH,1,2);
diffPost=diff(Post,1,2);
meandiffPre=mean(diffPre);
meandiffBH=mean(diffBH);
meandiffPost=mean(diffPost);
scatter(meanPre,diffPre,'Marker','x','MarkerEdgeColor','r')
hold on
scatter(meanBH,diffBH,'Marker','x','MarkerEdgeColor','b')
scatter(meanPost,diffPost,'Marker','x','MarkerEdgeColor','m')
hold off
end
Thanks,
Marine
  2 Comments

Sign in to comment.

Accepted Answer

Voss
Voss on 18 Nov 2022
Maybe something along these lines will get you started:
datapath = '.';
files = dir(fullfile(datapath,'*.xlsx'));
figure();
hold on
markers = 'xo';
colors = 'rbm';
n_files = numel(files);
h = zeros(3,n_files);
for ii = 1:n_files
file = fullfile(datapath,files(ii).name);
Pre = readtable(file,'Sheet',"Pre"); %sheet 1 of dataset1
Pre=table2array(Pre(:,["Var3","Var8"]));
BH = readtable(file,'Sheet',"BH"); %sheet 2 of dataset1
BH=table2array(BH(:,["Var3","Var8"]));
Post = readtable(file,'Sheet',"Post"); %sheet 3 of dataset1
Post=table2array(Post(:,["Var3","Var8"]));
meanPre=mean(Pre,2);
meanBH=mean(BH,2);
meanPost=mean(Post,2);
diffPre=diff(Pre,1,2);
diffBH=diff(BH,1,2);
diffPost=diff(Post,1,2);
meandiffPre=mean(diffPre);
meandiffBH=mean(diffBH);
meandiffPost=mean(diffPost);
h(:,ii) = [ ...
scatter(meanPre,diffPre,'Marker',markers(ii),'MarkerEdgeColor',colors(1)) ...
scatter(meanBH,diffBH,'Marker',markers(ii),'MarkerEdgeColor',colors(2)) ...
scatter(meanPost,diffPost,'Marker',markers(ii),'MarkerEdgeColor',colors(3)) ...
];
end
legend_str = string({files.name}) + " " + ["Pre"; "BH"; "Post"];
% % in older versions of MATLAB, do this instead:
% legend_str = strcat(repmat({files.name},3,1),{' '},repmat({'Pre';'BH';'Post'},1,n_files))
legend(h(:),legend_str, ...
'Interpreter','none', ...
'NumColumns',n_files)

More Answers (0)

Categories

Find more on Historical Contests in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by