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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by