showing error This still showing error like... Error in imshow (line 198) [common_ar​gs,specifi​c_args] = ... Error in' roi_demo (line 31) imshow(spr​intf('C:\U​sers\shrad​dha\Docume​nts\MATLAB​\Snaps\%d.​png',k)) >>'

2 vues (au cours des 30 derniers jours)
using the code which you gave me for dynamic image name is showing error
This still showing error like...
Error in imshow (line 198) [common_args,specific_args] = ...
Error in roi_demo (line 31) imshow(sprintf('C:\Users\shraddha\Documents\MATLAB\Snaps\%d.png',k)) >>

Réponses (2)

Geoff Hayes
Geoff Hayes le 23 Nov 2014
Shraddha - you should include the complete error message when posting your question. Looking at your code, if I were to try and run just
k = 2;
filename = sprintf('C:\Users\shraddha\Documents\MATLAB\Snaps\%d.png',k);
the warning message
Warning: Control Character '\U' is not valid. See 'doc sprintf' for control characters valid in the format
string.
is generated and the filename is simply
filename =
C:
Your string is a path to a file which includes backslashes. The backslash is a control character for fprintf and sprintf, so if you wish your string to have this character then you have to re-write your sprintf as
k = 42;
filename = sprintf('C:\\Users\\shraddha\\Documents\\MATLAB\\Snaps\\%d.png',k);
Now, your filename will be correct
filename =
C:\Users\shraddha\Documents\MATLAB\Snaps\42.png

Image Analyst
Image Analyst le 23 Nov 2014
You can either use double backslashes like Geoff showed:
imshow(sprintf('C:\\Users\\shraddha\\Documents\\MATLAB\\Snaps\\%d.png',k))
or use forward slashes. Yes, Windows handles forward slashes just fine:
imshow(sprintf('C:/Users/shraddha/Documents/MATLAB/Snaps/%d.png',k))
I still think that either one of those is bad form - too complicated. Personally I and Geoff recommend getting the filename in advance. It will make the code simpler to read and to maintain. I'd do it this way:
folder = 'C:\Users\shraddha\Documents\MATLAB\Snaps\';
baseFileName = sprintf('%.png',k);
fullFileName = fullfile(folder, baseFileName);
if exist(fullFileName , 'file')
imshow(fullFileName);
else
warningMessage = sprintf('Warning: %s does not exist', fullFileName);
uiwait(warndlg(warningMessage));
end

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by