Storing data in cell array using for loop

43 vues (au cours des 30 derniers jours)
JessHmann
JessHmann le 17 Nov 2019
Commenté : Vandit Bhayani le 17 Mai 2020
Hello
I am trying to store data in a cell array (called dataBase) using a for loop. For some reason only the last iteration of the loop is saved. The first two rows of the array remain empty. Any help is much appreciated :)
Below is my code:
for n=1:3
t = input('Enter an integer between 3 and 6: ');
while (t<3 || t>6)
t = input('Integer was too small/big. Please enter a number between 3 and 6.');
end
alpha = ([0:pi/t:2.*pi]);
si = sin(alpha);
co = cos(alpha);
M =[rad2deg(alpha); si ; co]';
x=M(:,1)';
ys=M(:,2)';
yc=M(:,3)';
dataBase=cell(3,3);
dataBase{n,1}=x;
dataBase{n,2}=ys;
dataBase{n,3}=yc;
end
  1 commentaire
Vandit Bhayani
Vandit Bhayani le 17 Mai 2020
Modifié(e) : Vandit Bhayani le 17 Mai 2020
I also have another Question regarding this. Need help urgently. How can I Plot the Graphs using a Subplot using this Code? I need to Plot three Graphs with Legends and all Titles and all.
This is my Code:
a = plot(dataBase.alpha, dataBase.si,'g');
b = plot(dataBase.alpha, dataBase.si,'rX');
c = plot(dataBase.alpha, dataBase.si,'b');
%%Drei Graphen werden hier geplottet. Datenbank wurde die Werte von Sinus und Cosinus speichern.
subplot(3,1,n);
plot(dataBase.alpha, dataBase.si,'g',dataBase.alpha, dataBase.si,'rX',dataBase.alpha, dataBase.si,'b');
grid on
axis on
title(['Dies ist der',numm2str(index),'.te Graph.'])
xlabel('Winkel(Grundmass)')
ylabel('Sinus,Cosinus')
legend([a c],{'Sinus','Cosinus'})
hold on

Connectez-vous pour commenter.

Réponse acceptée

Ajay Kumar
Ajay Kumar le 17 Nov 2019
Modifié(e) : Ajay Kumar le 17 Nov 2019
You are creating a new cell array everytime n is incrementing, which leads to loss of previous data. so, create the cell array outside for loop.
dataBase=cell(3,3);
for n=1:3
t = input('Enter an integer between 3 and 6: ');
while (t<3 || t>6)
t = input('Integer was too small/big. Please enter a number between 3 and 6.');
end
alpha = ([0:pi/t:2.*pi]);
si = sin(alpha);
co = cos(alpha);
M =[rad2deg(alpha); si ; co]';
x=M(:,1)';
ys=M(:,2)';
yc=M(:,3)';
dataBase{n,1}=x;
dataBase{n,2}=ys;
dataBase{n,3}=yc;
end
  2 commentaires
JessHmann
JessHmann le 17 Nov 2019
Thank you very much!!
Vandit Bhayani
Vandit Bhayani le 17 Mai 2020
I also have another Question regarding this. Need help urgently. How can I Plot the Graphs using a Subplot using this Code? I need to Plot three Graphs with Legends and all Titles and all.
This is my Code:
a = plot(dataBase.alpha, dataBase.si,'g');
b = plot(dataBase.alpha, dataBase.si,'rX');
c = plot(dataBase.alpha, dataBase.si,'b');
%%Drei Graphen werden hier geplottet. Datenbank wurde die Werte von Sinus und Cosinus speichern.
subplot(3,1,n);
plot(dataBase.alpha, dataBase.si,'g',dataBase.alpha, dataBase.si,'rX',dataBase.alpha, dataBase.si,'b');
grid on
axis on
title(['Dies ist der',numm2str(index),'.te Graph.'])
xlabel('Winkel(Grundmass)')
ylabel('Sinus,Cosinus')
legend([a c],{'Sinus','Cosinus'})
hold on

Connectez-vous pour commenter.

Plus de réponses (1)

Bhaskar R
Bhaskar R le 17 Nov 2019
In your case for each for loop new empty dataBase cell creating thats why you didn't get the stored result as you require. Always initialize empty or null variables outside of the loop.
dataBase=cell(3,3); % initialize befor the for loop
for n=1:3
t = input('Enter an integer between 3 and 6: ');
while (t<3 || t>6)
t = input('Integer was too small/big. Please enter a number between 3 and 6.');
end
alpha = ([0:pi/t:2.*pi]);
si = sin(alpha);
co = cos(alpha);
M =[rad2deg(alpha); si ; co]';
x=M(:,1)';
ys=M(:,2)';
yc=M(:,3)';
% dataBase=cell(3,3); % here your mistake
dataBase{n,1}=x;
dataBase{n,2}=ys;
dataBase{n,3}=yc;
end

Catégories

En savoir plus sur Graphics Performance dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by