How to make a function that loads its own input variables?
Show older comments
Hi there, I'm rather new to MATLAB and I'm trying to make a function which will have lots of potential input variables. For this reason I do not want to load them all to the workspace before I run the function. I'm sure I'm being dense, but there must be a way for a function to load its own input variables from a file on the path?
Hopefully this will clear up my aims:

Many thanks for any help
Accepted Answer
More Answers (3)
James Tursa
on 31 Oct 2016
Start with this:
doc save
doc load
Although, you could put everything in a struct and then pass that one struct (instead of passing many individual variables). Then extract the struct fields inside your function.
John D'Errico
on 31 Oct 2016
0 votes
Shiver. Kind of a bad idea. You can do it, but a poor choice of interface as you learn to program. Frequent loads are going to be slow, something you will not enjoy doing too often.
Better is to put them all into one .mat file. Then you could do one load. But if you are going to do that, you still will need to use load. And having a load inside your functions is again poor programming, as it is slow.
So better is to store all of your data in one structure. Then you can just pass it into your function as a single argument, and you avoid all of those spurious loads.
Archie
on 31 Oct 2016
0 votes
Categories
Find more on Whos in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!