Using a string in an if statement to print data from excel.

2 次查看(过去 30 天)
Hello,
I am trying to get data from an excel sheet to print from user in put (yes/no) this is what i have if anyone has any suggestions please let me know thank you!
  1 个评论
Stephen23
Stephen23 2021-11-17
@Haley Kelly: a much more robust approach is to use STRCMPI, just as Yongjian Feng showed.
Unlike the approach you are trying to use with ==, STRCMPI will work for different input types (so makes your code more robust) as well as handling different character cases (so it does not matter if your user enters "YES" or "yes").
You could even do this:
STRNCMPI(view_data,'yes',numel(view_data))
which would allow your user to enter any of "Y", "y", "YES", or "yes".
The approach you are trying to use is fragile and does not offer that flexibility.

请先登录,再进行评论。

采纳的回答

Yongjian Feng
Yongjian Feng 2021-11-17
yes is a string
if strcmpi(view_data, 'yes')
  3 个评论
Stephen23
Stephen23 2021-11-17
编辑:Stephen23 2021-11-17
"Also, fprintf writes into a file."
Only if the first argument is a file identifier to an open file (which in this case it is not).
Haley Kelly
Haley Kelly 2021-11-17
@Yongjian Feng @Stephen thank you both so much... disp worked perfectly! I feel so dumb haha... thank you again!

请先登录,再进行评论。

更多回答(1 个)

Jon
Jon 2021-11-17
编辑:Jon 2021-11-17
put single or double quotes around yes
if view_data == 'yes'
and also around no in the following line
  6 个评论
Stephen23
Stephen23 2021-11-17
编辑:Stephen23 2021-11-17
"i am still struggling it to get the table to print if the user inputs yes... any suggestions?"
Your FPRINTF call will not work as you probably expect.
To use FPRINTF effectively you should use a format string as the first argument: what format string to use depends on how you want the data to be displayed, as there are many options about number formatting, spaces, field widths, newlines, and more... You might have to generate the format string based on the size of that matrix and spend some time reading about how to specify a suitable number format. It is certainly possible, but only you can decide if it is worth the effort.
But I suspect that you actually just want to see the matrix displayed "as a matrix" in the command window. In that case, use DISP instead of FPRINTF.
Or don't use any function to display, just remove the semi-colon at the end of the line:
data(1:39,1:18) % no display function!
Jon
Jon 2021-11-17
Yongjian's use of strcmpi is much better. The expression view_data == 'yes' actually yields a 1 x 3 logical array which in the case that view_data really is assigned to the string 'yes' will be given by [1 1 1] (true true true) .
The if statement will handle this ok, and only pass if all the elements of the logical vector are true, but it is nicer to actually do a string comparison using strcmpi, this will also guard against problems with case sensitivity. Note that if the user had responded with the string 'Yes' instead of 'yes', then view_data=='yes' would equal [0 1 1] and the if statement would consider the expression to be false.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by