MATLAB Answers


[Simulink/Matlab] How to access variable from M-file?

Asked by Natt
on 3 Oct 2011


I created a model that does channel coding and it employs bpsk and channel repetition.

I have 3 variables that are inputs. snr, rep, frame_size

Where snr is signal to noise ratio, rep is number of repetitions, and frame_size is, well, the frame_size. This isn't really important but figured I would paint a picture...

I want to perform an analysis where I try to find the bit error rate (ber) while varrying the signal to noise ratio and repetitions--for each frame size.

From the command line, I set the snr, rep, and frame_size. Let's say my model is called channel_coding, I then run sim('channel_coding').

Low and behold ber pops up because it is set to go to the workspace as a variable called ber in the model.

So, I want to write a little script that collects all of this data while varying the parameters I mentioned. However, if I set the variable in the m-file, the model doesn't know and if I set the variable at the command line the m-file doesn't know but the model does.

I have tried making the variables snr, rep, and frame_size 'global'. That didn't work.

I also tried using simset() and that didn't work.

So, my friends. Would someone please share with me this secret that the bowels of the internet is hiding from me?


How are the three variables snr, rep and frame_size used in the model? Are they used by any blocks as parameters?

What is "Low and behold ber pops up"?

@Fangjun Jiang

1) "How are the three variables snr, rep and frame_size used in the model? Are they used by any blocks as parameters?"
--snr: snr is used as the signal to noise ratio value in the AWGN block
--rep: rep is used as the number of times to repeat each bit (for repetition coding) in a custom subsystem block
--frame_size: frame_size is used as the parameter for the Bernoulli binary generator

So, before I run my model, I set each of these variable values in the base workspace.

2) What is "Low and behold ber pops up"?
ber: ber is the 1x3 row vector that is the output of the error rate calculation.

In a clean workspace, with only snr, rep, and frame_size set, ber is the output to the workspace after the model runs.

Log in to comment.


1 Answer

Answer by Fangjun Jiang on 4 Oct 2011

In that case, I don't see any reason why you can't run the following script, just an example:

for k=1:10


@Fangjun Jiang
There is no particular reason it has to be a script but I started off that way and now I have some scar tissue to help me later on. Thanks so much for the help. You are a gentleman and a scholar.
@Kaustubha Govind
Indeed, I did try to use simset while calling the model with sim(). However, I must have not understood the syntax because I got the same error.

@Kaustubha Govind
That's the ticket! Thanks!

Use simset() is a better solution if your MATLAB version support that. Try to use that as much as you can. Then you don't need to change from M-function to M-script anymore.

Log in to comment.

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today