MATLAB Answers

Storing values from a for loop

1 ビュー (過去 30 日間)
Rylan Thronburg
Rylan Thronburg 2019 年 4 月 24 日
回答済み: Murugan C 2019 年 6 月 4 日
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 件のコメント

表示 3 件の古いコメント
Rylan Thronburg
Rylan Thronburg 2019 年 4 月 25 日
That doesn't change anything, the loops still only save the last input.
Walter Roberson
Walter Roberson 2019 年 4 月 25 日
Works for me. See attached.
Rylan Thronburg
Rylan Thronburg 2019 年 4 月 25 日
Thank you! Works like a charm, I just didn't see where the outputs were saved.

Sign in to comment.

回答 (1 件)

Murugan C
Murugan C 2019 年 6 月 4 日
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 件のコメント

Sign in to comment.

タグ

製品


リリース

R2018b

Translated by