how to use ismember() to check if an inputted number exists in a matrix.

14 Ansichten (letzte 30 Tage)
I have a csv file named "NaiveBankData.csv" (attached) and i imported it, then read it as a matrix, but now how can i check if an inputted number exists in the first colomn of the matrix or not.
the code i used for the first step is:
%import file
importdata('NaiveBankData.csv');
%read file as matrix
readNaiveBankData=readmatrix('NaiveBankData.csv', 'ThousandsSeparator', ',');
then i need to use:
%inputted number
AN = input('Enter account number');
if AN exists in the first colomn of "readNaiveDataBank"
How do i do this last part in code?

Akzeptierte Antwort

DGM
DGM am 3 Dez. 2021
This might be a start:
% read file as table, since a matrix can't hold both datatypes
Data = readtable('NaiveBankData.csv');
% inputted number
AN = input('Enter account number: ');
accountexists = ismember(AN,Data.AccountNumber);
if accountexists
accountbalance = Data.Balance(AN==Data.AccountNumber)
end
If you want to do it all with numeric data, you'll have to deal with the fact that the file contains currency data with literal formatting. This can be done using other import tools, but I'm just going to post-process the text in the table.
% read file as table, since a matrix can't hold both datatypes
Data = readtable('NaiveBankData.csv');
% convert to numeric arrays
account = Data.AccountNumber;
balance = cellfun(@(x) x(all(x~=('£,')',1)),Data.Balance,'uniform',false);
balance = str2double(balance);
% inputted number
AN = input('Enter account number: ');
accountexists = ismember(AN,account);
if accountexists
accountbalance = balance(AN==account)
end
  26 Kommentare
Stephen23
Stephen23 am 10 Dez. 2021
Bearbeitet: Stephen23 am 10 Dez. 2021
"I seem to remember @Jan having provided a regexprep() expression that did similar work."
Perhaps on this thread:
Walter Roberson
Walter Roberson am 10 Dez. 2021
In the main command window, use Preferences -> Command Window -> Text Display -> Numeric format and select "Long g". Also Preferences -> Variables -> Format -> default array format and select "Long g"
You might have to close the variable browser and re-open it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by