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

조회 수: 14 (최근 30일)
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?

채택된 답변

DGM
DGM 2021년 12월 3일
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
Stephen23
Stephen23 2021년 12월 10일
편집: Stephen23 2021년 12월 10일
"I seem to remember @Jan having provided a regexprep() expression that did similar work."
Perhaps on this thread:
Walter Roberson
Walter Roberson 2021년 12월 10일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by