I need a loop for an unknown amount of variables

조회 수: 2 (최근 30일)
Christina Falconer
Christina Falconer 2018년 4월 27일
답변: Prajit T R 2018년 4월 30일
So here is my problem. I am having the user pick an account from a listdlg box, and then they will enter an expense to subtract from that account. That will then calculate what is already in the account (from an excel spreadsheet) and then do the calculation and put the remaining balance into the proper cell in excel. So, I know how to make it do all of this, my issue is I don't know how many accounts they will have. So they could pick account 1 through account 200. I need to be able to make a loop that will do the calculation and pick the correct cell from excel for that account to do the calculations and then put the balance back into the correct cell. Here is what I have so far...
function edit(~,~)
[E N] = listdlg('PromptString','Pick an Account','SelectionMode','Single', 'ListString',accounts);
if E == 1
acctblc = begbalance(2,3);
acctblc=str2mat(acctblc);
expense = inputdlg('Expense to subtract');
expense = str2mat(expense);
endblc = begbalce -expense;
xlswrite('budgetworkbook1.xlsx',endblc,1,D2);
I now realize my if statement wont work because I have no idea how many accounts they will have set up. So the accounts will be in order on the listdlg box how they are in excel. So if they pick account 1, its in cell A1, the beginning balance in is B1, the remaining balance is in C1, etc. help?
  댓글 수: 3
Christina Falconer
Christina Falconer 2018년 4월 28일
Yes, correct.
Sergey Kasyanov
Sergey Kasyanov 2018년 4월 30일
Are you need code which allows to choose a one account from excel then enter expense and then save result in the same excel file and that may be repeated many times?

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

답변 (1개)

Prajit T R
Prajit T R 2018년 4월 30일
Hi Christina
I am attaching a code fragment which would help you:
data=xlsread('bank_data.xlsx')
accounts={'1','2','3','4','5','6'};
[E N] = listdlg('PromptString','Pick an Account','SelectionMode','Single', 'ListString',accounts);
dedn=input('Enter amount to deduct:')
data(E,2)=data(E,2)-dedn; %2 because account balance is in the second column
data
This is the simplest approach I could think of. The value held by the variable data in my example is:
data =
1 11000
2 12000
3 11500
4 11800
5 15000
6 14400
where 1 to 6 are the account numbers and the corresponding value is the account balance. You can make changes as per your requirement and explore this further. Once this is working fine, you can write the variable 'data' back to the Excel sheet.
Hope this helps.
Cheers

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by