How to extract input variable name when calling to GUI (GUIDE) function?
1 view (last 30 days)
Show older comments
Mark Golberg
on 19 Sep 2020
Commented: Mark Golberg
on 21 Sep 2020
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 Comments
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.
Accepted Answer
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:
3 Comments
Rik
on 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
More Answers (1)
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.
7 Comments
Walter Roberson
on 20 Sep 2020
did you read the link I posted in https://www.mathworks.com/matlabcentral/answers/596593-how-to-extract-input-variable-name-when-calling-to-gui-guide-function#comment_1016314
??
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.
See Also
Categories
Find more on Computer Vision with Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!