How to make a function that loads its own input variables?
显示 更早的评论
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
采纳的回答
更多回答(3 个)
James Tursa
2016-10-31
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
2016-10-31
0 个投票
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Whos 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!