Database Input Skipping Rows

5 Ansichten (letzte 30 Tage)
Michael Doherty
Michael Doherty am 8 Nov. 2016
Bearbeitet: Michael Doherty am 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.

Akzeptierte Antwort

KSSV
KSSV am 8 Nov. 2016
Instead of trying
C = length(customerDatabase)+1;
Try :
C = size(customerDatabase,1)+1;
  1 Kommentar
Michael Doherty
Michael Doherty am 8 Nov. 2016
Bearbeitet: Michael Doherty am 8 Nov. 2016
It worked! Thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by