0


This might be a pretty basic question but I was not able to find an answer for this.

I am trying to run a matlab code that has two arguments passed to it :
1. Name of an existing matlab code
2. An integer number.
This would be the command I would run in the command window in Matlab-

>> auto_caller mytest.m 10

Here auto_caller is the main code and it should execute the file mytest.m 10 times.

This is what I tried -

function [arguments] = auto_caller(filename, iterations)
    for i = 0 : str2num(iterations)
         filename
    end
end

But when I run it, the output is just -

mytest.m
mytest.m
mytest.m
mytest.m
mytest.m
mytest.m
mytest.m
mytest.m
mytest.m
mytest.m

Can someone help me to solve this problem?
Thanks,
Shreedhar

0

3 Answers 3

2

Instead of passing the name of the matlab source file containing the function, pass a function handle:

auto_caller @mytest 10

This is the same way that Mathworks-provided optimization algorithms accept goal functions, GUI widgets accept event callback functions, etc.

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

Comments

1

A simple solution:

Use run(scriptname_here)

3 Comments

I never knew there was a run function. +1 for teaching me something new
FWIW, run is just a wrapper for evalin and cannot accept input arguments.
@excaza that is interesting as well. thanks for that info.
0

You can call a file by using the eval function

function runScript(file,n)
    for i = 1:n
       eval(strrep(file,'.m','')) 
    end
end

3 Comments

'eval' works for me!! In fact I tried all the answers and they all work.
However, in the loop, we need to convert the value of n into number. str2num. Because using n as it is gave me an error.
with the way you are passing values into the function, yes you would need to convert it. I was assuming you could call the function as runScript('somefile',10)

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.