For loop problem for automatisation

3 次查看(过去 30 天)
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