How to extract input variable name when calling to GUI (GUIDE) function?
Ältere Kommentare anzeigen
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
Walter Roberson
am 19 Sep. 2020
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.
Mark Golberg
am 20 Sep. 2020
Walter Roberson
am 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. http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
Mark Golberg
am 20 Sep. 2020
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.
Akzeptierte Antwort
Weitere Antworten (1)
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
Mark Golberg
am 20 Sep. 2020
Steven Lord
am 20 Sep. 2020
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.
Mark Golberg
am 20 Sep. 2020
Image Analyst
am 20 Sep. 2020
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.
Mark Golberg
am 20 Sep. 2020
Walter Roberson
am 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.
Mark Golberg
am 21 Sep. 2020
Kategorien
Mehr zu Annotations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!