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

1 次查看(过去 30 天)
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 个评论
Natt
Natt 2011-10-3
@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.

请先登录,再进行评论。

回答(1 个)

Fangjun Jiang
Fangjun Jiang 2011-10-4
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 个评论
Fangjun Jiang
Fangjun Jiang 2011-10-4
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Test and Measurement 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by