For loop problem for automatisation

조회 수: 2 (최근 30일)
Laura Beal
Laura Beal 2020년 5월 26일
댓글: Laura Beal 2020년 5월 29일
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
Laura Beal
Laura Beal 2020년 5월 28일
That is ok, I use num2str and it work well.
Thanks a lot.
Laura Beal
Laura Beal 2020년 5월 28일
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

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

채택된 답변

William Alberg
William Alberg 2020년 5월 28일
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
Laura Beal
Laura Beal 2020년 5월 29일
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.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by