How do you extract the actual name of your input variable in a function?

4 ビュー (過去 30 日間)
Alex
Alex 2014 年 10 月 30 日
コメント済み: Alex 2014 年 10 月 30 日
I need to extract the actual name of my input variable for naming a file. Here is my simple script.
function bmp=array2bmp(a)
bmp=uint8(a);
nam=strcat(varname(a),'.bmp');
imwrite(bmp,nam,'bmp');
end
and varname is a function defined as such
function n=varname(var)
n=inputname(1);
end
so if I have an array named 'testbmp' and I type
bmp=array2bmp(testbmp);
My output is a .bmp file named 'a.bmp' not 'testbmp.bmp'. This does not make sense to me. Is there a way I can call the actual name of my actual input variable instead of whatever dummy variable I wrote in the function?

採用された回答

Geoff Hayes
Geoff Hayes 2014 年 10 月 30 日
Alex - it does make sense since you are passing a variable named a into your function varname. To get the variable name testbmp, you would have to call inputname(1) from the within the function that "receives" this variable as an input I.e. array2bmp.
  1 件のコメント
Alex
Alex 2014 年 10 月 30 日
This worked. Thank you for your detailed explanation which helped me understand the reasoning behind the problem.

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

その他の回答 (1 件)

Robert Cumming
Robert Cumming 2014 年 10 月 30 日
can you not just do this:
function bmp=array2bmp(a)
bmp=uint8(a);
nam=strcat(inputname(1),'.bmp');
imwrite(bmp,nam,'bmp');
You dont need your sub function varname

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by