A file name must be supplied

2 Ansichten (letzte 30 Tage)
med-sweng
med-sweng am 24 Dez. 2015
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.

Antworten (3)

Walter Roberson
Walter Roberson am 24 Dez. 2015
"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 Kommentare
Image Analyst
Image Analyst am 24 Dez. 2015
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 am 3 Mai 2020
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

Melden Sie sich an, um zu kommentieren.


karim botros
karim botros am 22 Apr. 2019
Bearbeitet: karim botros am 22 Apr. 2019
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 Kommentare
Walter Roberson
Walter Roberson am 25 Apr. 2019
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 am 3 Mai 2020
At the time of the original question, string objects did not exist yet in MATLAB

Melden Sie sich an, um zu kommentieren.


Ahmed Wageh
Ahmed Wageh am 3 Mai 2020
Try this form
imwrite(image, convertStringsToChars(Name+"_Modified.jpg"));
  1 Kommentar
Walter Roberson
Walter Roberson am 3 Mai 2020
imwrite automatically converts string objects in all recent versions (I can't promise it did in R2016b)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by