Storing data in cell array using for loop

43 ビュー (過去 30 日間)
JessHmann
JessHmann 2019 年 11 月 17 日
コメント済み: Vandit Bhayani 2020 年 5 月 17 日
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 件のコメント
JessHmann
JessHmann 2019 年 11 月 17 日
Thank you very much!!
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
  1 件のコメント
JessHmann
JessHmann 2019 年 11 月 17 日
Thank you :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by