For loop problem for automatisation

2 views (last 30 days)
Hello,
I am trying to automatize a calculation on Matlab.
I lot the correlation between colum 1 and 2 of my excel file.
But how can I increment the value of my colum, so I can oberve the correlation between column 2 and 3; and 3 and 4 etc...
This is my code :
theFile = 'Mise en forme réponse.xlsx';
page = 4;
Results = 'C2:G11';
matriceValue = xlsread(theFile, page, Results);
infos = {'Douceur revers' ; 'Epaisseur revers' ; 'Froissé' ; 'Déformable' ; 'Contact agréable'};
figure(4)
grid on;
boxplot(matriceValue,infos);
yticks(0:0.5:10)
xtickangle(45)
ytickformat('%.1f')
ylim([0 10]);
x = matriceValue(:,1);
y = matriceValue(:,2);
p = polyfit(x,y,1);
f = polyval(p,x);
figure(5)
plot(x,y,'o',x,f,'-')
title('Correlation between question 6 et question 7')
ylabel('Question 7')
ylim([0 10])
xlim([0 10])
xlabel('Question 6')
I know I could simply copy the last paragraph, but I want to know if it is possible to automatize this.
Thanks for your answers.
  4 Comments
Laura Beal
Laura Beal on 28 May 2020
That is ok, I use num2str and it work well.
Thanks a lot.
Laura Beal
Laura Beal on 28 May 2020
Sorry,
I have a new question, so I will try here.
Now I am trying to change my title in function on I.
leFichier = 'Mise en forme réponse.xlsx';
page = 4;
Resultat = 'C2:G11';
matriceValeurs = xlsread(leFichier, page, Resultat);
infos = {'Douceur revers' ; 'Epaisseur revers' ; 'Froissé' ; 'Déformable' ; 'Contact agréable'};
figure(4)
grid on;
boxplot(matriceValeurs,infos);
yticks(0:0.5:10)
xtickangle(45)
ytickformat('%.1f')
ylim([0 10]);
for i = 1:5
x = matriceValeurs(:,i);
y = matriceValeurs(:,i+1);
p = polyfit(x,y,1);
f = polyval(p,x);
figure(5+i)
plot(x,y,'o',x,f,'-')
title(['Correlation between question' ,infos(1), ' et question' ,infos(2)])
ylabel(['Question ', num2str(i+6)])
ylim([0 10])
xlim([0 10])
xlabel(['Question ', num2str(i+5)])
end
For the title I say infos(1) and infos(2) because I want to write "douceurs revers" and "epaisseur revers" in my title.
Do you know how I can do, because it doesent work ^^
Thanks a lot

Sign in to comment.

Accepted Answer

William Alberg
William Alberg on 28 May 2020
My error code is:
Index in position 2 exceeds array bounds
(must not exceed 5).
Error in main (line 17)
y = matriceValeurs(:,i+1);
I guess you get something similar
The fault is that "matriceValuers" only has 5 columns, and you try to acess a column 6
Since "infos" also have 5 values, i guess that 5 columns is the correct amount
The solution is change the for-loop so that it goes from 1:4
for i = 1:4
x = matriceValeurs(:,i);
y = matriceValeurs(:,i+1);
p = polyfit(x,y,1);
f = polyval(p,x);
figure(5+i)
plot(x,y,'o',x,f,'-')
title(['Correlation between question' ,infos(1), ' et question' ,infos(2)])
ylabel(['Question ', num2str(i+6)])
ylim([0 10])
xlim([0 10])
xlabel(['Question ', num2str(i+5)])
end
  1 Comment
Laura Beal
Laura Beal on 29 May 2020
Thanks a lot. It was exactly that.
I didn't realize that i+1 = 6 when i is 5 ^^.
Thanks a lot for your answer.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!