Database Input Skipping Rows

5 views (last 30 days)
Michael Doherty
Michael Doherty on 8 Nov 2016
Edited: Michael Doherty on 8 Nov 2016
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.

Accepted Answer

KSSV
KSSV on 8 Nov 2016
Instead of trying
C = length(customerDatabase)+1;
Try :
C = size(customerDatabase,1)+1;

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!