Database Input Skipping Rows

조회 수: 5 (최근 30일)
Michael Doherty
Michael Doherty 2016년 11월 8일
편집: Michael Doherty 2016년 11월 8일
So I have two functions, one that add customers and one that deletes customers to a .mat database. When the cell is empty and a customer is added, it starts at 2 (which is understandable), but when a second customer is added, it jumps to 7. I'm not quite sure why it keep doing this.
fprintf('\nPlease provide the following:')
customername = input('\nCustomer Name: ','s');
customerphone = input('Phone #: ','s');
customeraddress = input('Address: ','s');
customerpoints = input('Loyalty Points: ');
load('customerDatabase.mat');
C = length(customerDatabase)+1;
customerDatabase{C,1}=C+1;
customerDatabase{C,2}=customername;
customerDatabase{C,3}=customerphone;
customerDatabase{C,4}=customeraddress;
customerDatabase{C,5}=customerpoints;
save('customerDatabase.mat','customerDatabase')
input('Loyalty Customer added! ...Press ENTER to continue...');
Output:
customerDatabase =
[2] 'Test 1' '111-111-1111' '11 111th St' [ 111]
[] [] [] [] []
[] [] [] [] []
[] [] [] [] []
[] [] [] [] []
[7] 'Test2' '222-222-2222' '22 22ns Ave' [22222]
Press ENTER to continue...
Then if I try to remove customer 7 using
load('customerDatabase.mat');
customerDatabase
removecustomer = input('Which Lolalty Customer would you like to remove? (enter customer#): ');
for i = 1:length(customerDatabase)
if removecustomer == customerDatabase{i,1}
customerDatabase(i,:) = {[] [] [] [] []}; % delete row i from cell
emptyCells = cellfun('isempty', customerDatabase);
customerDatabase(all(emptyCells,2),:) = [];
for i = 1:length(customerDatabase);
customerDatabase{i} = 1 + i;
end
save('customerDatabase.mat','customerDatabase')
input('Loyalty Customer Removed... Press ENTER to continue...')
It completely changes the array to output this when I view the content of the .mat database:
customerDatabase =
[2] [3] [4] [5] [6]
Any help would be greatly appreciated.

채택된 답변

KSSV
KSSV 2016년 11월 8일
Instead of trying
C = length(customerDatabase)+1;
Try :
C = size(customerDatabase,1)+1;
  댓글 수: 1
Michael Doherty
Michael Doherty 2016년 11월 8일
편집: Michael Doherty 2016년 11월 8일
It worked! Thank you!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by