Is it possible write subroutines with input arguments in MATLAB?
Afficher commentaires plus anciens
So I dont want a function with definite output variables, but I want input arguments for my script.
For example I want to load data inside this subroutine/script. As far as I see, this cannot be done inside a function, because variables are local, but I want my loaded variables remaining. However using a script I cannot pass arguments.
Réponses (3)
Roger Stafford
le 30 Mai 2016
1 vote
You should be able to use a matlab function for that purpose. The information that you use as input to call on it remains in your system. Any information about what you loaded can be output from your function. Why do you have difficulties with that?
9 commentaires
Mr M.
le 30 Mai 2016
Mr M.
le 30 Mai 2016
Steven Lord
le 30 Mai 2016
Have your function return a struct array rather than a large number of outputs that are only known at runtime.
Image Analyst
le 31 Mai 2016
Don't use run(), just call the name. It can't be a script so you'll have to have this as the first line in myloadingscript.m:
function outputStructure = myloadingscript(input1, input2)
Then when you call it, you do
outputStructure = myloadingscript(input1, input2)
Honestly, no offense, but since you've been using MATLAB now for at least a year and a half, this is the kind of thing you should know already. Most people learn how to create and call functions on day 1 or 2 of learning MATLAB. Might want to review the Getting Started part of the documentation.
Mr M.
le 31 Mai 2016
Mr M.
le 31 Mai 2016
Yes: use one structure. You have been given the advice three times, from three people, that the best solution would be to use one structure. One of those people works for TMW (who make MATLAB), another is the highest ranked volunteer on this forum: do you imagine that they would give you bad advice?
No: do not "create" lots of variables like that. That would be about the worst solution that you could come up with. You should not "make" variables like that. This has been explained to you multiple times:
Image Analyst
le 31 Mai 2016
It is possible to have functions that handle variable numbers of input and output arguments. Look up varargin and varargout in the help. Usually though there is up to a known number of arguments that have a known meaning, not just any old arbitrary thing you might throw in there. Like it can take up to 10 arguments and it looks to see, for example, if arg #8 is there and if it is, then it represents the LineWidth, or whatever it might be expecting. Many MATLAB functions use this with a name,variable pair. It looks at the argument and if arg 4 is, say, 'LineWidth' then it will expect arg #5 (the next one) to be an integer number that represents the line width.
Steven Lord
le 31 Mai 2016
Yes it is possible but it's HIGHLY discouraged. That way lies "poofing" variables into your workspace, which could lead to different behavior than you expect possibly with no indication that you're not getting the result you think you're getting.
For example, if your MAT-file had a variable named beta that contained the vector [5 6 7], what do you think this code would do?
function myfun
load myMatFileContainingBeta
disp(beta(1, 3))
You're probably going to say that you would expect it to display 7. It won't. It will display 0.333333333333333 instead as per the second example on this documentation page. [I left out the "|format rats|" call which is why it doesn't display as 1/3.]
The way I would write this would be:
function myfun
data = load('myMatFileContainingBeta');
disp(data.beta(1, 3))
Then you can specify whatever fields you wish, and your calling function can check them and use them as appropriate. A structure is the easiest and most robust way to pass variables, when those variables are not known beforehand.
Walter Roberson
le 31 Mai 2016
The answer you are looking for is
function loadmydata(filename)
try
datastruct = load(filename);
catch
fprintf('File does not exist or cannot be loaded: "%s"', filename);
return
end
fn = fieldnames(datastruct);
for K = 1 : length(fn)
thisvar = fn{K};
assignin('caller', thisvar, datastruct.(thisvar));
end
Sooner or later this is going to cause you problems, when you accidentally overwrite an existing variable, or when you start using static workspaces for efficiency.
1 commentaire
Stephen23
le 1 Juin 2016
@Walter Roberson: sadly the advice that "sooner or later this is going to cause you problems" seems to be often ignored by beginners.
Catégories
En savoir plus sur Variables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!