how to rum m file multiple times automatically?
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Stephen23
le 18 Juin 2023
"how to rum m file multiple times automatically?"
Have you tried using a FOR loop?
Réponses (2)
Rik
le 18 Juin 2023
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 commentaires
Aniketh
le 18 Juin 2023
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 commentaires
Steven Lord
le 18 Juin 2023
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.
Voir également
Catégories
En savoir plus sur Startup and Shutdown dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!