how to rum m file multiple times automatically?

16 visualizzazioni (ultimi 30 giorni)
mariam muner
mariam muner il 18 Giu 2023
Commentato: Stephen23 il 18 Giu 2023
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 Commento
Stephen23
Stephen23 il 18 Giu 2023
"how to rum m file multiple times automatically?"
Have you tried using a FOR loop?

Accedi per commentare.

Risposte (2)

Rik
Rik il 18 Giu 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.

Aniketh
Aniketh il 18 Giu 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:
  1. 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.
  2. After all runs have finished, Line 11 uses the matlab -batch option to save the results array to a MATLAB file named results.mat
  3. 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 Commenti
mariam muner
mariam muner il 18 Giu 2023
i'm using windows so i think i need batch file
but i'm not that expert with it. if you can give me step i'll be thankful
thank you
Steven Lord
Steven Lord il 18 Giu 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.

Accedi per commentare.

Categorie

Scopri di più su Entering Commands in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by