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

Hello,
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?

5 Kommentare

That seems to be confusing the name of the variable with the content of the variable. The content of the variable would probably be the file name. If not then you probably cannot assume that the name of the variable has anything to do with the name of the video.
There is no file name.
I simply want to load a matrix the size of (H x W x numOfFrames).
Lets assume matrix name is : "myMatVideo".
I'm typing the following command:
Video_Player(myMatVideo) --- > and the content of "myMatVideo" goes to varargin cell withing my GUI.
I can show it's frames, etc...
Now, I have a text field in the GUI, where I want it to be automatically be written -
"The following video was loaded: myMatVideo".
Thus, my question is:
how can I tell the main function of the GUI, Video_Player, was called with the variable "myMatVideo".
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. http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
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?
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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 20 Sep. 2020

0 Stimmen

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:

3 Kommentare

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);
end
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)?
You can't do it like that. You need to use this:
Video_Player(moose)
function Video_Player(var)
varname=inputname(1);
if isempty(varname)
%deal with inputs like Video_Player(1:10) here
end
%rest of the code here
end
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};
end
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 !!!
P.S
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...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Steven Lord
Steven Lord am 20 Sep. 2020

1 Stimme

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.

7 Kommentare

Hey Steven, thank you for your comment.
But, there is NO file.
Imagine this, you have a workspace with the following variables:
var_1 , var_2 , var_3....
Now, in the command window, you can open your GUI with any of the following options:
Video_Player(var_1) / Video_Player(var_2) / Video_Player(var_3) / ....
Let's assume you called it: Video_Player(var_42).
Then, I'd like it to be written in some text object within my GUI, something like "... var_42 was loaded...".
Again, no files! Only variables.
Can this be done somehow?
Use inputname as Walter said, but you should be prepared for the case where the input has no name like
Video_Player(1:10)
In that case there is no way in general to identify that the expression used to create the input was 1:10 inside Video_Player.
As I’ve answered to Walter, I don’t fully understand how to combine inputname with the calling of Video_Player.
Can you please write an explicit code?
  • I don’t mind about the case of Video_Player(1:10).
I agree with the other experts here - what you're doing is bad practice. You should pass in the name. One thing you could do if you have a bunch of arbitrarily-named variables (which goes against the advice in the FAQ) is to just list those names, as you get them, into a listbox.
listBoxItems = handles.lstVideos.String; % var1, var2, var3, etc.
listBoxItems{end+1} = 'var42'; % Tack on new name when you have it.
Then the user clicks on the name (var3 or var42 or movieOfMine or whatever its name is) and then pass that string to the other function along with the variable.
listBoxItems = handles.lstVideos.String; % var1, var2, var3, etc.
selectedItem = handles.lstVideos.value;
selectedName = listBoxItems{selectedItem};
switch selectedItem
case 1
Video_Player(var1, selectedName) ;
case 2
Video_Player(var2, selectedName) ;
case 3
Video_Player(var3, selectedName) ;
end
I don't know how you're getting these names, but it would be better if you had a constant, fixed set of them, like var1 through var42 and not have them be something that's unknown when you're writing the program, and known only the the end user, like "marksVideo" or whatever.
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 -
Video_Player(general_name_variable)
  • 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?
THANk YOU !!!
??
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
Good-Grief.mat
23Skidoo.mat
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.
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).

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by