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

1 Ansicht (letzte 30 Tage)
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
Mark Golberg
Mark Golberg am 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 am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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:
  3 Kommentare
Rik
Rik am 20 Sep. 2020
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
Mark Golberg
Mark Golberg am 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};
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
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
Walter Roberson
Walter Roberson am 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
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.
Mark Golberg
Mark Golberg am 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).

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