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

Cust(x).monall = monall;
Rylan Thronburg
Rylan Thronburg 2019년 4월 24일
Where do I place this? I tried putting it in the first for loop and the second one, it doesn't do anything either way.
Walter Roberson
Walter Roberson 2019년 4월 25일
After the for y loop but before the end of the for x loop.
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일
편집: 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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Murugan C
Murugan C 2019년 6월 4일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2018b

태그

질문:

2019년 4월 24일

답변:

2019년 6월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by