New file name with multiple variables

So I made a code in Matlab and i want to save my file with a custom name. But for some reason I can't seem to be able to add multiple variables to the name. This is the code that I have. What am I doing wrong?
baseFileName = [name,'_Freesbeeld','D',R,'H',Height,'Dpi',dpi,'.tif'];
%If I use the example below it works
%baseFileName = [name,'_Freesbeeld','.tif'];
fullFileName = fullfile(savelocation, baseFileName);
imwrite(W, fullFileName); % img respresents input image.

 Accepted Answer

Tom Osborne
Tom Osborne on 3 Sep 2018
Edited: Tom Osborne on 3 Sep 2018
The variable outputs need to be filename friendy - i.e. a string without any illegal characters.
Therefore, you might need to use int2str( variable ) and similar functions to ensure this is the case.

1 Comment

Thanks!
My variables were indeed numeric and needed to be changed to a string!

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 3 Sep 2018
Edited: Stephen23 on 3 Sep 2018
"What am I doing wrong?"
MATLAB does not implicitly convert the numeric dpi to the string that represents that number, it converts the numeric to the character with that character code, e.g. 1 -> char(1). It is unlikely that this is what you intended, so you need to do the conversion explicitly yourself, e.g. using num2str or int2str as Tom Osborne suggested, or even better by using sprintf to define the whole filename in one go:
baseFileName = sprintf('%s_Freesbeeld_D%d_H%d_Dpi%d.tif',name,R,Height,dpi)
You will have to check that he format string is correct for your data types, as you did not tell us anything about them.

Categories

Products

Release

R2018a

Community Treasure Hunt

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

Start Hunting!