A file name must be supplied

조회 수: 10 (최근 30일)
med-sweng
med-sweng 2015년 12월 24일
댓글: Walter Roberson 2020년 5월 3일
I have the following imwrite statement:
imwrite(img, strcat(thisdir,'_',num2str(j),'_LABEL_',categoryClassifier.Labels(labelIdx),'.jpg'));
where categoryClassifier.Labels(labelIdx) returns a string, and thisdir is also a string returning the directory name. But, I get the following error:
Error using imwrite>parse_inputs (line 510)
A filename must be supplied.
Error in imwrite (line 418)
[data, map, filename, format, paramPairs] = parse_inputs(varargin{:});
Isn't the part from strcat considered the filename?
Thanks.

답변 (3개)

Walter Roberson
Walter Roberson 2015년 12월 24일
"where categoryClassifier.Labels(labelIdx) returns a string"
No it does not, it returns a cell array containing a string. The strcat() of a string and a cell array gives you a cell array result, which is not valid for imwrite.
I also recommend you switch to using sprintf() rather than strcat
imwrite(img, sprintf('%s_%d_LABEL_%s.jpg', thisdir, j, categoryClassifier.Labels{labelIdx}) );
  댓글 수: 2
Image Analyst
Image Analyst 2015년 12월 24일
I'd further recommend that you simplify this by not having one massively long line of code and split it up into smaller lines of code that are easier to follow:
% Create the base filename for the PNG file.
baseFileName = sprintf('%d_LABEL_%s.jpg', j, categoryClassifier.Labels{labelIdx}) );
% Prepend the folder:
fullFileName = fullfile(thisdir, baseFileName);
% Write out the image:
imwrite(img, fullFileName);
Doing things in multiple steps will often let you discover your errors easier because it will be easier to debug.
Also note I used a PNG format because you should not use JPG format files for image analysis - they often have bad JPEG artifacts which greatly alter your data.
Finally med-sweng I'd recommend you read the FAQ on cell arrays http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F - it might help you get a better intuitive feel for what they are, how they work, and how to use them (like when to use braces, parentheses, and brackets).
Walter Roberson
Walter Roberson 2020년 5월 3일
note that at the time I wrote the above, matlab did not have string objects, so where I referred to string it was character vector

댓글을 달려면 로그인하십시오.


karim botros
karim botros 2019년 4월 22일
편집: karim botros 2019년 4월 22일
I agree with Mr Roberson about the cause of the problem but not the solution.
I think what you need to do is to convert this cell array of strings into one string, in order to do so:
value= string (strjoin (value));
while 'value' is the cell array of strings.
  댓글 수: 2
Walter Roberson
Walter Roberson 2019년 4월 25일
However, you do not have only a string array: you also have numeric value, j.
You could try
strjoin( {thisdir, '_', j, '_LABEL_', categoryClassifier.Labels{labelIdx}, '.jpg' } )
but that will fail because j is not a character vector. It will also fail if you try
strjoin( {string(thisdir), "_", string(j), "_LABEL_", string(categoryClassifier.Labels{labelIdx}), ".jpg"} )
because strjoin() cannot work on cell array of string() objects.
It would work to use
strjoin( [thisdir, "_", j, "_LABEL_", categoryClassifier.Labels{labelIdx}, ".jpg"] )
but this will turn out to have an extra space between each component, so you would have to add the parameter '' at the end of the strjoin() call.
At this point you might as well leave out the strjoin and instead use
thisdir + "_" + j + "_LABEL_" + categoryClassifier.Labels{labelIdx} + ".jpg"
This is a valid method that some people prefer over using sprintf() .
One advantage of sprintf() over using + with string objects is that sprintf() permits you to easily specify the format for the numeric conversion. For example it is common in file names to use 3 digits with leading 0 for numeric values, such as '009' and '078' instead of '9' and '78' . Using the + string() operator does not give you that ability. So you have to start using the compose() function.. but the compose() function is effectively just sprintf() except that it proceeds by rows instead of by columns...
Walter Roberson
Walter Roberson 2020년 5월 3일
At the time of the original question, string objects did not exist yet in MATLAB

댓글을 달려면 로그인하십시오.


Ahmed Wageh
Ahmed Wageh 2020년 5월 3일
Try this form
imwrite(image, convertStringsToChars(Name+"_Modified.jpg"));
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 5월 3일
imwrite automatically converts string objects in all recent versions (I can't promise it did in R2016b)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by