pathoctavebackslash

How do I keep backslashes from being removed in a user input in Octave?


I'm writing a script that requires the user to copy and paste a file path as an input, which obviously includes several backslashes. This is stored as a string, but for some reason Octave is automatically removing the backslashes and concatenating the rest.

For example: This\Is\An\Example\FilePath becomes ThisIsAnExampleFilePath.

I need the backslashes in order for my script to actually use the file path and find the file. How can I get Octave to stop removing them?

PROMPT FOR USER TO INPUT FILEPATH:

filePath = input("File Path (Surround with single quotes): ");

I have not been able to come up with any solutions as of yet.


Solution

  • Take a look at the documentation for input.

    The way you use it, the user’s input is evaluated as an Octave expression. This is why the user must add the quotes around their path. Octave strings are evaluated such that the backslash is an escape character. \n is a new line, for example.

    The other way, where the second argument to the function is "s", the user’s input is taken as a string, not evaluated. In this mode, the user doesn’t need to add quotes, and their backslashes will not be seen as escape characters.