Azzera filtri
Azzera filtri

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

12 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

DGM
DGM il 3 Dic 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 Commenti
Stephen23
Stephen23 il 10 Dic 2021
Modificato: Stephen23 il 10 Dic 2021
"I seem to remember @Jan having provided a regexprep() expression that did similar work."
Perhaps on this thread:
Walter Roberson
Walter Roberson il 10 Dic 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.

Accedi per commentare.

Più risposte (0)

Tag

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by