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

2 次查看(过去 30 天)
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 个评论
Image Analyst
Image Analyst 2022-11-18
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:

请先登录,再进行评论。

采纳的回答

Voss
Voss 2022-11-18
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)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by