Garima Singh
Garima Singh

Reputation: 1490

Matlab: How to print " ' " character

I am trying to create the following string:

javaaddpath ('C:\MatlabUserLib\ParforProgMonv2')

However, I could only do the following

command = sprintf('%s ', varargin{1}, '(', varargin{2}, ')');

and that gives me:

javaaddpath ( C:\MatlabUserLib\ParforProgMonv2 )

UPDATE: Based on Dan's suggestion, I used the following:

command = sprintf('%s', varargin{1}, '(', '''', varargin{2}, '''', ')')

Upvotes: 1

Views: 555

Answers (2)

DrBwts
DrBwts

Reputation: 3657

There are a couple of ways around this. First you could declare your path as a string variable then pass the string to your command, eg,

path = 'my/path'
javaaddpath (path)

Or you can use special characters to insert things like a single quote or a new line character, so for a single quote,

EDIT: wrong display command as pointed out by Dan below

myString = '" Hi there! "' 
disp(myString)

Upvotes: 1

Dan
Dan

Reputation: 45752

Use two single quotation marks. See the docs for formatting strings, btw this concept is known as an escape character (to help you google such things in the future).

command = sprintf('%s ', varargin{1}, '(''', varargin{2}, ''')')

Although I think you might prefer

command = sprintf('%s (''%s'')', varargin{1}, varargin{2})

or if you have no other varargins (which I guess is very unlikely but anyway)

command = sprintf('%s (''%s'')', varargin{:})

Upvotes: 3

Related Questions