How to pull data from a table from a file and use as a string or cell.

3 ビュー (過去 30 日間)
Terry Carney
Terry Carney 2022 年 11 月 9 日
コメント済み: Terry Carney 2022 年 11 月 10 日
Hello,
I'm building a app via App Designer. The app allows the user to use a numeric keypad to enter a 6-digit passcode and submit that code. I have a file that holds demo data with a person's first name, last name, and their assigned code.
What I want to do is to create a ui message that says "hello, {firstname} {lastname}" when their passcode is entered correctly. I'm trying to use
readtable
to pull the table from the "loginInfo.txt" file, and write code to pull the code's first and last name on its respected row. This is what I have, minus the app design:
function startupFcn(app)
T = readtable('loginInfo.txt') % printing out for debugging. Thought about creating table to pull firstName
% and lastName cell/string
msgbox("Hello, Please Enter Identification Passcode:");
end
% Button pushed function: confirm
function confirmButtonPushed(app, event)
codeString = string(app.passCode.Value);
msgbox(codeString);
% A = fileread("loginInfo.txt") Trying ideas
% B = strfind(A, codeString);
% if a correct passcode is discovered
if ~isempty(B)
uialert(uifigure,"Yes",codeString); %My potential "Hello, {firstName} {lastName}" message.
end
% assignin("base",'fcnStatus',codeString);
T = readtable output
T =
4×3 table
LastName FirstName PassCode
____________ __________ __________
{'Doe' } {'John' } 4.3222e+05
{'Doe' } {'Jane' } 3.2901e+05
{'Williams'} {'John' } 7.1883e+05
{'Smith' } {'Taylor'} 5.7162e+05

採用された回答

Karim
Karim 2022 年 11 月 9 日
Hi Terry,
See below for one example on how to do this. This idea is to first use some logic operators to determine if the code is valid or not, and then extracting the user id with the logic information.
T = readtable("https://nl.mathworks.com/matlabcentral/answers/uploaded_files/1186583/loginInfo.txt")
T = 4×3 table
LastName FirstName PassCode ____________ __________ __________ {'Doe' } {'John' } 4.3222e+05 {'Doe' } {'Jane' } 3.2901e+05 {'Williams'} {'John' } 7.1883e+05 {'Smith' } {'Taylor'} 5.7162e+05
% asume the user entered
Code = 329011;
% use logic operators to find the code in the list
CurrentUser = T.PassCode == Code
CurrentUser = 4×1 logical array
0 1 0 0
if any(CurrentUser)
% a valid code has been entered, now print the name of the user
msgbox( "hello "+T.FirstName{CurrentUser}+" "+T.LastName{CurrentUser});
else
% no valid code was found... break the routine
msgbox('Code not recognized')
end
  2 件のコメント
Terry Carney
Terry Carney 2022 年 11 月 10 日
for
CurrentUser = T.PassCode == Code
I had to convert T.PassCode to a string
string(T.PassCode)
is there a reason the code runs for you and not for me? Thank you for your help!!
Terry Carney
Terry Carney 2022 年 11 月 10 日
Nevermind, I answered my own question. Thank you again for your help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by