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.
采纳的回答
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!