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 Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

릴리스

R2018a

질문:

2018년 9월 3일

댓글:

2018년 9월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by