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

조회 수: 1 (최근 30일)
Marpe
Marpe 2022년 11월 18일
댓글: Voss 2022년 11월 19일
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:
Marpe
Marpe 2022년 11월 18일
My bad, I just added the data

댓글을 달려면 로그인하십시오.

채택된 답변

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에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by