Borrar filtros
Borrar filtros

For loop problem for automatisation

1 visualización (últimos 30 días)
Laura Beal
Laura Beal el 26 de Mayo de 2020
Comentada: Laura Beal el 29 de Mayo de 2020
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 comentarios
Laura Beal
Laura Beal el 28 de Mayo de 2020
That is ok, I use num2str and it work well.
Thanks a lot.
Laura Beal
Laura Beal el 28 de Mayo de 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

Iniciar sesión para comentar.

Respuesta aceptada

William Alberg
William Alberg el 28 de Mayo de 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 comentario
Laura Beal
Laura Beal el 29 de Mayo de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by