Database Input Skipping Rows

5 次查看(过去 30 天)
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;

更多回答(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