please help with work

조회 수: 2 (최근 30일)
Dominic Garcia
Dominic Garcia 2020년 11월 18일
편집: Rena Berman 2021년 5월 7일
I have an assignment and I need it done. I am stuck and am very weak with strings...
  댓글 수: 4
Rik
Rik 2020년 11월 18일
편집: Rik 2020년 11월 18일
Tackle the problem part by part:
"Your program must be able to ask user to enter a variable name"
Did you Google how you can do that?
"and display the user entered name"
Read the documentation for fprintf (or disp)
"check against the above rules"
You can do this two ways: test agains the entire list of allowed characters, or use Matlab functions that test properties of chars.
You get the idea. Which part did you try? Given that it is urgent, you must have already used Google, right?
Rik
Rik 2020년 11월 19일
편집: Rena Berman 2021년 5월 7일
Why did you remove the question body?
I'll be posting the removed content from here.

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

채택된 답변

Sean de Wolski
Sean de Wolski 2020년 11월 18일
v = ["Not Valid" "Valid"];
v(isvarname(input('enter variable name: ', 's'))+1)
  댓글 수: 2
Rik
Rik 2020년 11월 18일
Nice one, a valid solution to the actual problem and very compact. And yet OP will still have to think for themselves to come up with an answer that their instructor will accept. Have an upvote from me.
Sean de Wolski
Sean de Wolski 2020년 11월 18일
I think this would also work elegantly for the engine:
potentialvarnames = ["hello" "he_llo" "2sw" "w?2" "x"];
matches(potentialvarnames, lettersPattern(1)+asManyOfPattern(alphanumericsPattern|characterListPattern("_")))

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

추가 답변 (1개)

James Tursa
James Tursa 2020년 11월 18일
편집: James Tursa 2020년 11월 18일
Use the input( ) function to get the user input. Use the optional 's' argument to get the input as a char string.
I'm assuming you can use the ASCII table provided to decide what range a character is in. Note that numbers and letters are in contiguous sections, which allows you to do the following:
Suppose you have stored the user input in a variable called vname. Then
vname(1) >='A' && vname(1) <= 'Z'
would check to see if the first character is an uppercase letter.
vname(1) >='a' && vname(1) <= 'z'
would check to see if the first character is a lowercase letter.
vname(1) >='0' && vname(1) <= '9'
would check to see if the first character is a decimal digit.
vname(1)=='_'
would check to see if the first character is an underscore
Etc.
See if you can write some looping code based on the number of characters in vname to check all of this.
  댓글 수: 2
Dominic Garcia
Dominic Garcia 2020년 11월 18일
thank you. I am progressing through now, but am still unsure on how to check if any element in my string is a letter or number
e.g. i need to check if it contains any numbers, what would it be in that case, because vname() would just produce all characters.
James Tursa
James Tursa 2020년 11월 18일
The basic idea would be to write a loop:
for k=1:numel(vname)
% code to check the vname(k) character here
end

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

카테고리

Help CenterFile Exchange에서 Install Products에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by