m-files as function inputs in for loop

조회 수: 3 (최근 30일)
Sarah Andrade
Sarah Andrade 2018년 2월 5일
댓글: Greg 2018년 2월 15일
Hi everyone!
I have a function.m that I run with an input.m like this:
function input
I'm trying to process a Monte Carlo Samples in this function. So I have a lot of m-files inputs: input_1.m, input_2.m, ..., input_n.m.
To process everything I set up this for loop:
for i = 1:n
filename = sprintf('%s_%d', 'input', i);
function filename
end
But the function doesn't recognize the string to run. The errors are:
Error using pdatvp_seq (line 49)
Data file not found: filename.
Error in run_test (line 4)
function filename
Someone knows how to help me to do the function recognize the string to run?
  댓글 수: 4
Adam
Adam 2018년 2월 6일
편집: Adam 2018년 2월 6일
What does progcoll look like? I'm not familiar with trying to just pass one .m file in after another like that. Usually you would pass in arguments in parentheses. Also, trying to simplify your question is good, but doing so by replacing real function names with 'function' is very confusing since it is a fundamental key word.
Sarah Andrade
Sarah Andrade 2018년 2월 6일
편집: Sarah Andrade 2018년 2월 6일
It's a nonlinear structure analysis code. I'm not familiar with this too.
And sorry about the confusion with the names!

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2018년 2월 6일
편집: Stephen23 2018년 2월 6일
One obvious problem is that you are using command syntax whereas you need to be using function syntax (note the parentheses!):
for k = 1:n
fname = sprintf('input_%d.m', k);
progcoll(fname)
end
Your original code did not work because with command syntax all trailing strings are interpreted literally, so the trailing filename is not interpreted as a variable containing a name, but instead simply as the string of characters 'filename'. I would recommend that you always use function syntax every time that you call a function. Read about the difference here:
It states "When a function input is a variable, you must use function syntax to pass the value to the function. Command syntax always passes inputs as literal text and cannot pass variable values"
  댓글 수: 3
Stephen23
Stephen23 2018년 2월 6일
편집: Stephen23 2018년 2월 6일
"The parentesis didn't work here"
Did the code run? Did you get any warning or error message? (please give the complete message if you did)
Sarah Andrade
Sarah Andrade 2018년 2월 6일
I worked more in your solution and I discovered that I put the directories to the wrong path. When I fixed it, your code worked and the process started!!! Thank you so much, I'm trying to solve this for days :)
And sorry if I didn't make myselse clear before, english isn't my first language.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Kai Domhardt
Kai Domhardt 2018년 2월 6일
As a general description, you have a function you want to call in an .m file:
.m
function out = target_fun(in)
...
end
The function can be called, with input and output, by using the eval command
main.m
fun_call = [fun_name, '(' fun_param ')'];
result = eval(fun_call);
For your use you can apply it like this:
for i = 1:n
filename = sprintf('%s_%d', 'input', i);
eval(filename)
end
  댓글 수: 3
Sarah Andrade
Sarah Andrade 2018년 2월 6일
Kai Domhardt, It didn't work! Only the script of the input run. But thank you for your ideia.
Stephen Cobeldick, thank you for your explanation! I didn't know about this.
Greg
Greg 2018년 2월 15일
I just want to re-iterate:
Do not use eval! Which reminds me of another item I'd like to put in things I would not miss.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Monte Carlo Analysis에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by