Storing data in cell array using for loop

21 views (last 30 days)
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 Comment
Vandit Bhayani
Vandit Bhayani on 17 May 2020
Edited: Vandit Bhayani on 17 May 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

Sign in to comment.

Accepted Answer

Ajay Kumar
Ajay Kumar on 17 Nov 2019
Edited: Ajay Kumar on 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 Comments
Vandit Bhayani
Vandit Bhayani on 17 May 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

Sign in to comment.

More Answers (1)

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

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!