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

5 vues (au cours des 30 derniers jours)
Marpe le 18 Nov 2022
Commenté : Voss le 19 Nov 2022
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 commentairesAfficher AucuneMasquer Aucune
Image Analyst le 18 Nov 2022
It would be easier if you'd attach at least two files. Make it easy for us to help you, not hard.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Marpe le 18 Nov 2022

Connectez-vous pour commenter.

Réponse acceptée

Voss le 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)
2 commentairesAfficher AucuneMasquer Aucune
Marpe le 19 Nov 2022
That is exactly what I wanted thanks!
Voss le 19 Nov 2022
You're welcome!

Connectez-vous pour commenter.

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by