Ho to keep entries in a for loop

Hello hi i have a question how do i keep the entries in a for loop

Answers (1)

You are pre-initializing ele with a zero, that's why it gets stored in ASCII in every loop iteration. I would change your code to the following:
nbr_elem = input('Enter the total number of elements in the molecule: ');
ele = [];
occ = zeros(1, nbr_elem) %it is good practise to pre-initialize arrays with their full size
for x = 1:nbr_elem
ele = [ele, input('Enter an element: ','s')];
occ(x) = input('Enter the element''s occurrence in the molecule: ');
end
Also, I would suggest using
while ~strcmp(ele(x), table_periodique(j(x),2))
in your while loop instead of ~= There is also no need to index j(x), you can just set j = 1. Hope this helps!

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 4 Oct 2016

Edited:

on 4 Oct 2016

Community Treasure Hunt

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

Start Hunting!