Addee
Addee

Reputation: 671

sprintf not correctly creating a valid file path

I am trying to run the following code, however I am getting an error when trying to read the text file

DATAopts.imgsetpath = 'C:\Users\z5085693\Downloads\matconvnet-calvin-master\matconvnet-calvin-master\data\Datasets\VOC2010\VOCdevkit\VOC2010\ImageSets\Main\%s.txt';
trainName = 'train';

trainIms = textread(sprintf(DATAopts.imgsetpath, trainName), '%s'); 

This yields the error:

Error using textread (line 165)
File not found.

When I execute just the sprintf portion

sprintf(DATAopts.imgsetpath, trainName)

I simply get 'C:' which is where the "File not found" error is coming from because obviously that's not the full path.

What is happening and how can I fix it?

Upvotes: 2

Views: 920

Answers (1)

Suever
Suever

Reputation: 65460

The character '\' is an escape character therefore you'll want to escape each of these backslashes prior to calling sprintf to get an actual backslash in the result.

escaped_imgsetpath = strrep(DATAopts.imgsetpath, '\', '\\');
result = textread(sprintf(escaped_imgsetpath, trainName)); 

Upvotes: 2

Related Questions