0

The MATLAB builtin "LOAD" can be called using an argument (the filename) and it modifies the workspace by adding the loaded variable, even if there are no output arguments to the function.

I want to do the same with a custom function.

So I want to implement a custom load function, how do I do this?

4
  • What exactly do you want the function to do? And have you tried simply writing a script, scripts can alter the workspace without asking output arguments. Commented Oct 25, 2013 at 8:49
  • I want the same behaviour as the standard "load" function. So a script would work in principle but then how do I pass an argument to it (like load accepts it) Commented Oct 25, 2013 at 8:51
  • well, I'm not sure if I get this rigth, but do you know about the difference between a script and a function? if not: a function can accept parameters, just look for the syntax, search for it by typing: doc function Commented Oct 25, 2013 at 8:57
  • I suppose you would have to use a function then, see my updated answer. Commented Oct 25, 2013 at 8:58

2 Answers 2

4

Check the assignin and evalin function. That's exactly what you describe.

Sign up to request clarification or add additional context in comments.

3 Comments

I think that evalin should be sufficient.
In the question it is not specified, that the data is available as matlab save file. If he needs to do some parsing or preparation of it assignin might be more useful.
Yes I need some custom loading and preparation, so assignin is appropriate I guess. Thank you very much!
1

I am guessing what you want, but I suppose this may be it:

If you want to create the functionality like load create a function called myLoad

function myload(theString)
evalin(['load ' theString],'caller')

At least this should reproduce the basic functionality


To create a load script that loads file A from myDir1 and loads file B from myDir2 there is an easier way:

load fullfile(myDir1,A)
load fullfile(myDir2,B)

Just put them in a file called myLoad and make sure to save it as a script, not as a function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.