Do I need to escape characters in this MATLAB string?

12,186

Solution 1

This should work:

[status string]=system('grep "Up to" ~/test_linux/vision1.1/log | awk ''{print $7}'' ');

You have to escape ' with another ' if you want it to appear as a character in a string. With respect to handling strings in MATLAB, ' is the only character with special meaning (it starts and ends the string), so it is the only one that needs escaping.

Caveat: Some functions may interpret their string arguments in different ways, and thus require certain characters to be escaped in different ways. These requirements will appear in the documentation for each function. A couple of these types of functions off the top of my head:

Solution 2

You'll need to escape the single quotes in the command string. Otherwise MATLAB will interpret them as the end of the string, and then break down on the stuff that follows it.

Share:
12,186
Tim
Author by

Tim

Elitists are oppressive, anti-intellectual, ultra-conservative, and cancerous to the society, environment, and humanity. Please help make Stack Exchange a better place. Expose elite supremacy, elitist brutality, and moderation injustice to https://stackoverflow.com/contact (complicit community managers), in comments, to meta, outside Stack Exchange, and by legal actions. Push back and don't let them normalize their behaviors. Changes always happen from the bottom up. Thank you very much! Just a curious self learner. Almost always upvote replies. Thanks for enlightenment! Meanwhile, Corruption and abuses have been rampantly coming from elitists. Supportive comments have been removed and attacks are kept to control the direction of discourse. Outright vicious comments have been removed only to conceal atrocities. Systematic discrimination has been made into policies. Countless users have been harassed, persecuted, and suffocated. Q&A sites are for everyone to learn and grow, not for elitists to indulge abusive oppression, and cover up for each other. https://softwareengineering.stackexchange.com/posts/419086/revisions https://math.meta.stackexchange.com/q/32539/ (https://i.stack.imgur.com/4knYh.png) and https://math.meta.stackexchange.com/q/32548/ (https://i.stack.imgur.com/9gaZ2.png) https://meta.stackexchange.com/posts/353417/timeline (The moderators defended continuous harassment comments showing no reading and understanding of my post) https://cs.stackexchange.com/posts/125651/timeline (a PLT academic had trouble with the books I am reading and disparaged my self learning posts, and a moderator with long abusive history added more insults.) https://stackoverflow.com/posts/61679659/revisions (homework libels) Much more that have happened.

Updated on June 05, 2022

Comments

  • Tim
    Tim over 1 year

    I would like to call the following bash command in MATLAB:

    grep "Up to" ~/test_linux/vision1.1/log | awk '{print $7}'
    

    I use system() in MATLAB, but it turns out to have errors:

    >> [status string]=system('grep "Up to" ~/test_linux/vision1.1/log | awk '{print $7}' ');     
    ??? [status string]=system('grep "Up to" ~/test_linux/vision1.1/log | awk '{print $7}' ');  
    
    Error: Unbalanced or unexpected parenthesis or bracket.
    

    Do I need to escape some special characters in the bash command as a string in MATLAB?

  • Tim
    Tim almost 14 years
    Just wonder what is the escape character in Matlab? Is it always single quote? I remember in function printf(), it is \.
  • gnovice
    gnovice almost 14 years
    @Tim: I added some more detail about escape characters to the answer.