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

2 visualizaciones (últimos 30 días)
Haley Kelly
Haley Kelly el 17 de Nov. de 2021
Comentada: Jon el 17 de Nov. de 2021
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 comentario
Stephen23
Stephen23 el 17 de Nov. de 2021
@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.

Iniciar sesión para comentar.

Respuesta aceptada

Yongjian Feng
Yongjian Feng el 17 de Nov. de 2021
yes is a string
if strcmpi(view_data, 'yes')
  3 comentarios
Stephen23
Stephen23 el 17 de Nov. de 2021
Editada: Stephen23 el 17 de Nov. de 2021
"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 el 17 de Nov. de 2021
@Yongjian Feng @Stephen thank you both so much... disp worked perfectly! I feel so dumb haha... thank you again!

Iniciar sesión para comentar.

Más respuestas (1)

Jon
Jon el 17 de Nov. de 2021
Editada: Jon el 17 de Nov. de 2021
put single or double quotes around yes
if view_data == 'yes'
and also around no in the following line
  6 comentarios
Stephen23
Stephen23 el 17 de Nov. de 2021
Editada: Stephen23 el 17 de Nov. de 2021
"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 el 17 de Nov. de 2021
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by