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.

 Akzeptierte Antwort

Tom Osborne
Tom Osborne am 3 Sep. 2018
Bearbeitet: Tom Osborne am 3 Sep. 2018

0 Stimmen

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 Kommentar

Ben Aerens
Ben Aerens am 3 Sep. 2018
Thanks!
My variables were indeed numeric and needed to be changed to a string!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Stephen23
Stephen23 am 3 Sep. 2018
Bearbeitet: Stephen23 am 3 Sep. 2018

0 Stimmen

"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.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by