Storing data in cell array using for loop

21 Ansichten (letzte 30 Tage)
JessHmann
JessHmann am 17 Nov. 2019
Kommentiert: Vandit Bhayani am 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 Kommentar
Vandit Bhayani
Vandit Bhayani am 17 Mai 2020
Bearbeitet: Vandit Bhayani am 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Ajay Kumar
Ajay Kumar am 17 Nov. 2019
Bearbeitet: Ajay Kumar am 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 Kommentare
JessHmann
JessHmann am 17 Nov. 2019
Thank you very much!!
Vandit Bhayani
Vandit Bhayani am 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Bhaskar R
Bhaskar R am 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

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by