Storing values from a for loop
    4 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
I'd like to store multiple values from my for loop, though it only saves the last values. How do I rewrite the code to make it save all values? Here is what I have so far.
CustAmt = input('How many customers would you like to examine?: ');
for x = 1:CustAmt
    Cust(x).name = input('What is the name of this customer?: ','s');
    Cust(x).pamt = input('How many products do you sell to this customer?: ');
     monall = [];
     for y = 1:Cust(x).pamt 
         product = input('What is the name of this product?: ','s');
         fprintf('How much money is allocated for %s?',product);
         monall = [monall, input(': ')];
     end
end
I would like to save each input value for each customer.
6 comentarios
  Walter Roberson
      
      
 el 25 de Abr. de 2019
				
      Editada: Walter Roberson
      
      
 el 25 de Abr. de 2019
  
			Works for me. See attached.
Respuestas (1)
  Murugan C
      
 el 4 de Jun. de 2019
        Hi,
CustAmt = input('How many customers would you like to examine?: '); 
for x = 1:CustAmt 
    Cust(x).name = input('What is the name of this customer?: ','s'); 
    Cust(x).pamt = input('How many products do you sell to this customer?: '); 
    monall = [];
    prodnames = []; 
    for y = 1:Cust(x).pamt  
        product = input('What is the name of this product?: ','s');
        fprintf('How much money is allocated for %s?',product);
        prodnames = [prodnames, {product}]; monall = [monall, input(': ')]; 
    end 
    Cust(x).prodnames = prodnames; 
    Cust(x).monall = monall;
    save(Cust(x).name,'Cust') % store the variable into .mat data
end
0 comentarios
Ver también
Categorías
				Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

