how to rum m file multiple times automatically?
10 次查看(过去 30 天)
显示 更早的评论
Hello
if i have matlab file that measures the bit error rate for certain communication system i use rand function to generates bits and noise
i need to excuite the program like 1000 time for each noise value then record the results each time and find the average and plot it as final results
my question ish how to run the m file 1000 times and record the results of each time in a vector without running it manually
NOTE :- i'm not using function inside my code
1 个评论
Stephen23
2023-6-18
"how to rum m file multiple times automatically?"
Have you tried using a FOR loop?
回答(2 个)
Rik
2023-6-18
You already point at the solution in your question.
Any serious code you intend to use after next week should be in a function. That way you have a stable interface that you can document. It is also trivially easy to run a function in a loop, which is what you need to do.
0 个评论
Aniketh
2023-6-18
To execute an m-file multiple times automatically, you can use a shell script or a batch file depending on your operating system. Here's an example on how to use a shell script to run your MATLAB file multiple times:
#!/bin/bash
num_runs=1000
results=()
for i in $(seq 1 $num_runs)
do
output=$(matlab -nodesktop -nosplash -batch "run('your_MATLAB_file.m'); disp(ans); exit;" | tail -2 | head -1)
results=("${results[@]}" $output)
done
# Save the results to a MATLAB file
matlab -nodesktop -nosplash -batch "save('results.mat', 'results'); exit;"
Some explanation of the above code:
- The output of each run is stored in the output variable, which is obtained by piping the matlab command's output to tail -2 | head -1. This extracts the second-to-last line of output produced by MATLAB, which should contain the result you are interested in. You may need to modify the arguments to tail and head if your output is formatted differently.
- After all runs have finished, Line 11 uses the matlab -batch option to save the results array to a MATLAB file named results.mat
- Once the script completes, you can load the results.mat file into MATLAB using the load command. For example, if the script was run from the same directory as the results.mat file, you can load the file into MATLAB using: load('results.mat')
4 个评论
Steven Lord
2023-6-18
There's no need to write a shell script for the task of running a function multiple times in MATLAB. See the Topics section on this documentation page for examples of how to do this in MATLAB.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!