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.

 採用された回答

Tom Osborne
Tom Osborne 2018 年 9 月 3 日
編集済み: Tom Osborne 2018 年 9 月 3 日

0 投票

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 件のコメント

Ben Aerens
Ben Aerens 2018 年 9 月 3 日
Thanks!
My variables were indeed numeric and needed to be changed to a string!

サインインしてコメントする。

その他の回答 (1 件)

Stephen23
Stephen23 2018 年 9 月 3 日
編集済み: Stephen23 2018 年 9 月 3 日

0 投票

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

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

製品

リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by