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

Greetings!
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?

3 Comments

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.

Sign in to comment.

Answers (1)

In that case, I don't see any reason why you can't run the following script, just an example:
all_snr=rand(10,1);
all_rep=1:10;
all_frame_size=10:10:100;
result=zeros(10,3);
for k=1:10
snr=all_snr(k);
rep=all_rep(k);
frame_size=all_frame_size(k);
sim('channel_coding');
result(k,:)=ber;
end

12 Comments

You've hit the nail on the head, my friend.
When I try to access the output variable ber from the script, I get an error saying that it is unknown.
If you run the simulation manually (not using sim() command), after the simulation is completed, do you see ber in the base workspace?
Is your script inside a function? I assume you know the difference between M-function and M-script.
@Fangjun Jiang
"If you run the simulation manually (not using sim() command), after the simulation is completed, do you see ber in the base workspace?"
--If I run the simulation by pressing the play button in Simulink, I do see the variable ber in the base workspace.
"Is your script inside a function? I assume you know the difference between M-function and M-script."
--My script is inside a function called channel_coding.
I do know that my function has its own workspace, if that's what you mean. Is this my problem? Here is the code:
function ber_plot()
% We are going to end up making a plot of BER v SNR to each of the four
% frame sizes.
%snr = -6:1:10; % increase in 1 dB increments, up to 10 dB
%rep = 1:2:9; % increase in 2 bit increments, up to 9
%
% Make a separate plot for each frame size, but keep the variance of the
% other paramters the same for each case of frame size. So there will be
% a total of 4 plots.
%
frame_size = 64; % sequence = 64, 16, 128, 256
bit_err = [0 0 0];
for i = 1:4
switch i
case i == 2
frame_size = 16;
case i == 3
frame_size = 128;
case i == 4
frame_size = 256;
otherwise
frame_size = frame_size;
end
for j = -6:1:10
snr = j;
for k = 1:2:9
rep = k;
sim('channel_coding');
% need to create 4 different bit_err arrays for each frame size
bit_err = [bit_err; ber]
end
end
end
That is EXACTLY the problem!
A Simulink model takes the parameters and write the output to the 'base' workspace. In your ber_plot() function, you are changing the value of snr, rep and frame_size. However, all those values are set in the function workspace. When it runs to the sim() line, the simulation may be able to run but it is using whatever the value for snr, rep and frame_size in the 'base' workspace. The result is written to the 'base' workspace too. So when it runs to bit_err = [bit_err; ber] line, it says ber is not available. If you replace that line with evalin('base',ber), you will see what I mean.
/facepalm.
I had a feeling that was it. Unfortunately, I can't test it since I don't have the comm systems block set in my student version. So, let me make sure I understand.
In order to set or get a variable that is in the base workspace from the function work space I need to:
set -- assignin('base', snr); assignin('base', rep); ...
get -- evalin('base', ber)
Then I should be good to go?
You are right. The function to do that operation is assignin() and evalin(). Pay attention to the syntax of assignin() though. It is assignin('base','snr',0.5) for example.
However, I would recommend you use M-script for this task. Then you avoid the problem transferring data between base and function workspace. Any particular reason it has to be a function?
FYI: You can ask Simulink to look for parameter variables in the function workspace by using the simset command as described here: http://www.mathworks.com/matlabcentral/answers/10149-simulink-workspace
@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.

Sign in to comment.

Tags

Asked:

on 3 Oct 2011

Community Treasure Hunt

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

Start Hunting!