MATLAB Answers

How to extract input variable name when calling to GUI (GUIDE) function?

3 views (last 30 days)
I have a simple GUI for playing videos called: Video_Player(inputVar). Supposably
inputVar --- > is a variable called vidMat.
Now, when calling for Video_Player(vidMat), I have an edit object, where I would like to present the following text:
" The following video was loaded : _______".
I know my function was called with "vidMat", but how do I pass this ('vidMat') to become a string inside my GUI?


Show 2 older comments
Walter Roberson
Walter Roberson on 20 Sep 2020
Lets assume matrix name is : "myMatVideo".
Let's not assume that. That assumes that the name of the matrix to be presented to the user is going to relate to the name of the variable. That in turn assumes that every time you load or create a different matrix, that you are going to use a different variable name that has some meaning to the user. Unless you are going to be the only person ever using the code, the only way to carry that out with variable names meaningful to the user based upon user inputs (without modifying the code) would be to use dynamic variable names.
Mark Golberg
Mark Golberg on 20 Sep 2020
the name of the variable could be everything...
it can be : "table" or "cat" or "sfsdff" of "America", it doesn't really matter.
Yes, everytime I'll call Video_Player with different variable. Also, I won't even be the only one to use this.
Now, once Video_Player was called in the follwoing form:
Video_Player(table) or Video_Player(cat) or ....
I'd like to prepare something like:
set(handles.edit1 , 'String' , ['The following video was loaded: XXX'])
when XXX is the name of the variable (XXX = table or cat or America etc...)
Can this be done somehow?
Walter Roberson
Walter Roberson on 20 Sep 2020
Yes, it can be done easily. However, you should not do it. You should assume that people may loop using a constant variable name that is not the name of the video. If what is being passed is not a file name, then you should either permit the user to pass a name to use, or else you should omit that output as being probably meaningless.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 20 Sep 2020
Don't Do It.
But since you seem unlikely to understand that you have made a design mistake until you have had enough complaints from the users that your design is wrong, then the technical way to do it is:


Mark Golberg
Mark Golberg on 20 Sep 2020
Thank you Walter.
Can you please assist a bit further. I mean, as advised, I've prepared the function:
function out = getVarName(var)
out = inputname(1);
But, I don't know what to do next.
Let's assume my variable name is 'moose'.
How do I combine between: Video_Player(moose) & getVarName(moose)?
Rik on 20 Sep 2020
You can't do it like that. You need to use this:
function Video_Player(var)
if isempty(varname)
%deal with inputs like Video_Player(1:10) here
%rest of the code here
Mark Golberg
Mark Golberg on 20 Sep 2020
Thank you Rik.
Finally it works.
I've added the following lines to my code:
varName = inputname(1);
if ~isempty(varName)
varargin(1,3) = {varName};
Just before GUI initialization begins... you know :
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Video_Player_OpeningFcn, ...
'gui_OutputFcn', @Video_Player_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
So now, when I have a variable in my work space:
vidMat , size of (H x W x numOfFrames)
and I call for:
Video_Player(vidMat) -
I got at -
varargin(1,1) -- > the content of my input variable
varargin(1,3) -- > the name of my input variable.
Exactly what I wanted. THANKS A LOT !!!
Don't ask me, why I'm using (the 3rd index)
varargin(1,3) = {varName};
instead of (the 2nd index)
varargin(1,2) = {varName};
For some reason, the 2nd index gives an error in "gui_mainfcn" function... strange, but I don't really care...

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 20 Sep 2020
If you want your app to display the name of the file that was loaded, you should probably have your app accept the filename rather than a variable containing data that may or may not have been created by loading a file and may or may not even have a name. [In an expression like plot(1:10) the input to plot has no name.]
The app can then load the data from the file whose name was provided (thus ensuring the data was loaded from a file) and state from which file it personally loaded the data.


Show 4 older comments
Mark Golberg
Mark Golberg on 20 Sep 2020
Thank you Image Analyst. For some reason, I can't be clear enough about what I'm trying to do.
I don't fully understand where exactly I should add you code.
Think of it as follows:
it could be absolutely any variable !!! there is no list, as today it will be var_1, and tomorrow some other user would use it with var_427, and in a week from now, some second user would user with it with Video_Player(blablabla).
Because he insisted on saving his video matrix into a variable called 'blablabla'.
I'm adding 2 files here. Perhaps it would help.
Like in every GUI there is this section:
% --- Executes just before Video_Player is made visible.
and it's very easy for me to actually load the contents of my input variable, as follows:
handles.vidMat = varargin{1,1};
since I've opened my GUI by typing in command line -
  • general_name_variable = matrix of the size H x W x numOfFrames.
Now, similarly, I'd really love to be able to do the following:
set(handles.edit1 , 'String' , 'the following variable was loaded: general_name_variable').
Thus, I'd like to somehow extract the name of my input variable, because today it could be 'general_name_variable', while tomorrow it cold be 'moose123', and in a month, it could be 'abcde'.
  • Also, if possible, I'd really love to understand from someone of the experts, what exactly is "bad practice" in what I'm trying to do?
Walter Roberson
Walter Roberson on 20 Sep 2020
Suppose that I am a user of your function and I have three files that I want to display video for, and I want the title displayed to be correct. I used dir() to get the file names, and I am looping over the information that dir returns. dir() has no trouble telling me that the files are
Mighty Mouse!!!.mat
Now as the person coding the call to your video player function, what do I need to do in order to convince your function to display the correct video name, as the part of the file before the extension?
Your code would require that I look at the file names and dynamically create variable names to match them, and then call your function with the dynamic name, just in order to get the title correct.
thisfile = dinfo(k).name;
[~, vname, ~] = fileparts(thisfile) ;
datastruct = load(thisfile) ;
vdata = datastruct.Video;
eval( sprintf('%s=vdata;', vname));
eval( sprintf('Video_Player(%s);', vname));
Notice the two eval()... though you could get it to one by combining the commands.
Did you happen to notice that none of the three file names are valid variable names? So in reality you would have to process vname to fudge it to be a valid variable name, ending up with variable names such as Goodx3cGrief and that would be what the title would show up as.
No reasonable API should force a user to dynamically generate a variable name for the sake of getting a title to have to do with the intended content of the variable. And as I point out here, because variable names are limited, users cannot generate variable names that will give them good titles.
Again I recommend that you either permit the user to pass in the title or else you leave that out of the application.
Mark Golberg
Mark Golberg on 21 Sep 2020
Thank you Walter, but the use case yuo described is highly unlikely. Video_Player is a simple utilty to view videos. Video can be loaded through Browse button in the GUI (file must be an AVI, not mat), or directly from a work space variable, by typing - Video_Player(varName).
This is an analytics tool.
If user choose to load video from a file, my string would be simply a full path of that file (don't really care what the file name is).

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by