Ben Fossen
Ben Fossen

Reputation: 1007

Using sprintf in Matlab?

This is similar to a question I asked previously about opening a pdf in Matlab.

file = 'sl3_knt_1_2.pdf'
location = 'C:\Program Files\Tracker Software\PDF Viewer\PDFXCview.exe %s'
str = sprintf(location,file);
system(str)

This returns the warning:

Warning: Invalid escape sequence appears in format string. See help sprintf for valid escape sequences. 

I think it has something to the location variable getting read as an escape sequence since it uses \ but I am not sure. I cant seem to get this to work.

Upvotes: 3

Views: 11662

Answers (3)

Oli
Oli

Reputation: 16035

Alternatively, you can change your location string like that:

location = 'C:\\Program Files\\Tracker Software\\PDF Viewer\\PDFXCview.exe %s'

Usually \ is used for special characters. For instance \n is an end of line. So when you really want to write \, you need to escape it by using \. So, you need to write \\ in this case

Upvotes: 2

ypnos
ypnos

Reputation: 52317

The easy solution is to use '/' instead of '\', which works on all platforms, including Windows. '\' is problematic being a special character.

Upvotes: 3

AGS
AGS

Reputation: 14498

Try this:

file = 'sl3_knt_1_2.pdf'
location = 'C:\Program Files\Tracker Software\PDF Viewer\PDFXCview.exe'

str = sprintf('%s %s',location, file)

system(str)

Upvotes: 4

Related Questions