1

What I am trying to do is write a script in Matlab that will parse some HTML, get some data out of it, take this data, and then format it into a Javascript string array. I do not have a problem grabbing HTML and parsing it and so forth, I am having trouble trying to print a string using sprintf that will contain a backslash.

Basically if you have a string in Javascript that contains a quotation mark you need to escape it with a backslash:

var string1 = "Here is a \"string\" example"; 

When I try to do this in Matlab as follows, it does not print correctly:

>> A = sprintf('Here is a \"string\" example') A = Here is a "string" example 

This is not a valid Javascript string. So basically I want sprintf to return a proper Javascript string; any suggestions?

I have tried using \\" and \\\" and a few similar combinations, all to no avail.

4
  • 1
    @twerdster is correct, Strings are defined with single quotation marks and for inserting a backslash: formatting doc: To insert a backslash, use \\ Commented Oct 13, 2012 at 8:27
  • OK, we got off on the wrong foot here. I was just annoyed with how sloppy you had phrased the question/formatting, it was the end of a long day, etc., etc. So: apologies. If it helps: I was not trying to make myself feel better, I was trying to make you a better programmer; which includes good documentation skills. Commented Oct 16, 2012 at 11:39
  • Now, back to your question: judging from your comments, it seems this is a case of an XY problem. Can you show us how you "hacked" around it, so that we can solve the actual problem, not your take to the solution to it? Also, have you seen this? Commented Oct 16, 2012 at 11:39
  • My solution was to not use sprintf, I found that it was not necessary for what I wanted. I ended up using simple string concatenation and regular expressions. Commented Oct 18, 2012 at 18:46

2 Answers 2

1

Matlab uses the reference Kernighan, B. W., and D. M. Ritchie, The C Programming Language for its sprintf function. According to the reference, the way sprintf is defined, it uses escape character as a way to overcome the default control meaning of characters.

Although you asked not to suggest the combination like

\" \\\" 

here is the viable solution where you don't have to manually replace the \" with \\" and the code does this action itself.

a = "Here is a \"string\" example"; d = regexprep(a,"\"","\\\""); sprintf(d); 
Sign up to request clarification or add additional context in comments.

1 Comment

Strings in Matlab are single-quoted, double quotes have no syntactic meaning in Matlab.
0

Try

sprintf('Here is a \\"string\\" example.') 

On my machine this outputs

Here is a \"string\" example. 

2 Comments

For some reason that is not working for me properly. It may be because I am trying to do it combined with string concatenation and also printing to a file. I don't know. I was able to work around it in a kind of hack way, but whatever.
@MZimmerman6: Can you show us this hack way? Perhaps we can help you solve the issue instead of work around it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.