rndness

really random tech
Blog RSS
  • Android
  • software
  • utilities
  • Columbia University
  • mooc
  • Google
  • drones
  • Machine Learning
  • Coursera
  • T-Mobile
  • data science
  • firmware
  • FML
  • machine learning
  • Asus
  • Python
  • Ubuntu
  • Arduino
  • IoT
  • MOOC
  • LG G4
  • LG Cloud
  • electronics
  • PITA
  • iOS
  • Revision3
  • Future Labs
  • hack
  • DFT
  • MenuMaid
  • Microsoft
  • Johns Hopkins

Crazy Matlab Strings

February 26, 2016 in programming

I was writing some Matlab code when I realized something weird about the way special characters in strings are handled by different string methods.  One would think a string is a string is a string and that sprtinf, fprintf and input would handle strings similarly.  Wrong. 

I want to prompt the user for a selection using the prompt function.  This function takes a string as a parameter soI created a string that with a single tab but prompt simply ignores it.  Odd.

Below is some code where I create strings with tabs, new line and slashes.  I then try to print to console each string using fprintf and input.

prompt = 'Hello\tworld\n';
fprintf(prompt)
choice = input(prompt);

prompt = 'Hello\nworld\n';
fprintf(prompt)
choice = input(prompt);

prompt = 'Hello\\world\n';
fprintf(prompt)
choice = input(prompt);

In the case of the string with the tab, input doesn't recognize the tab but just prints the letter t between the word.  For the other two cases everything is just peachy keen.  Its like input will only interpret certain special characters. Why?

% testing tab
Hello	world
Hellotworld

% testing new line
Hello
world
Hello
world

% testing slash
Hello\world
Hello\world
← Google Hates Computer CodeFirst Lego League NYC →
Back to Top