Storing data in cell array using for loop

21 次查看(过去 30 天)
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 个评论
Vandit Bhayani
Vandit Bhayani 2020-5-17
编辑:Vandit Bhayani 2020-5-17
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

请先登录,再进行评论。

采纳的回答

Ajay Kumar
Ajay Kumar 2019-11-17
编辑:Ajay Kumar 2019-11-17
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 个评论
Vandit Bhayani
Vandit Bhayani 2020-5-17
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

请先登录,再进行评论。

更多回答(1 个)

Bhaskar R
Bhaskar R 2019-11-17
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

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by