I would like to create an array to store a password, website combo.
2 次查看(过去 30 天)
显示 更早的评论
I would like to write a function to enter strings of passwords and websites with each new entrie going to the next line of the array. However I am finding that the script I have now is overwriting the previous entries.
function passwordArray = addPasswordWithWebsite(passwordArray, password, website)
% Check if the passwordArray variable exists
% Add the website and password pair to the array
passwordArray{1, end+1} = website;
passwordArray{2, end+1} = password;
end
This is the code I have thus far. Also do I have to index the array in the main script each time, as when I try to run it after closing Matlab it says passwordArray variable is undefined.
1 个评论
Voss
2023-11-13
Notice that the code as shown adds two columns to passwordArray:
website = 'google.com';
password = 'PasSwOrD';
% let's say initially passwordArray has 0 columns:
passwordArray = cell(2,0);
% this adds a new column:
passwordArray{1, end+1} = website;
% and this adds another new column:
passwordArray{2, end+1} = password;
% so now there are two columns, one with the website and one with the
% corresponding password:
passwordArray
You can change the second end+1 to end:
% let's say initially passwordArray has 0 columns:
passwordArray = cell(2,0);
% this adds a new column containing the website:
passwordArray{1, end+1} = website;
% and this fills in the 2nd row in the last column with the password:
passwordArray{2, end} = password;
% so now there is one column:
passwordArray
Or add both the website and the password to the array at the same time:
% let's say initially passwordArray has 0 columns:
passwordArray = cell(2,0);
% this adds a new column containing both website and password:
passwordArray(:, end+1) = {website; password};
% so now there is one column:
passwordArray
回答(1 个)
Chunru
2023-11-14
Use string array instead of cell array for efficiency. You can also considre to use table.
pa = ["abc", "def"]
pa = addPasswordWithWebsite(pa, "123", "456")
function passwordArray = addPasswordWithWebsite(passwordArray, website, password)
% Check if the passwordArray variable exists
% Add the website and password pair to the array
passwordArray(end+1, :) = [website, password];
end
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!