[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 件のコメント
Fangjun Jiang
2011 年 10 月 3 日
How are the three variables snr, rep and frame_size used in the model? Are they used by any blocks as parameters?
Fangjun Jiang
2011 年 10 月 3 日
What is "Low and behold ber pops up"?
Natt
2011 年 10 月 3 日
回答 (1 件)
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 件のコメント
Natt
2011 年 10 月 4 日
Fangjun Jiang
2011 年 10 月 4 日
If you run the simulation manually (not using sim() command), after the simulation is completed, do you see ber in the base workspace?
Fangjun Jiang
2011 年 10 月 4 日
Is your script inside a function? I assume you know the difference between M-function and M-script.
Natt
2011 年 10 月 4 日
Fangjun Jiang
2011 年 10 月 4 日
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.
Natt
2011 年 10 月 4 日
Walter Roberson
2011 年 10 月 4 日
Yup.
Fangjun Jiang
2011 年 10 月 4 日
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?
Kaustubha Govind
2011 年 10 月 4 日
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
Natt
2011 年 10 月 4 日
Natt
2011 年 10 月 4 日
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.
カテゴリ
ヘルプ センター および File Exchange で Test and Measurement についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!