matlab from有什么用,Matlab函数使用'fromworkspace'将向量传递给simulink
我想編寫一個包含simulink塊的matlab函數 . 該函數應將數據加載到simulink模型中,運行它,然后從函數返回數據 .
我能想到的唯一方法就是在simulink中使用'To Workspace'和'From Workspace'塊 . 問題是'From Workspace'塊不從功能范圍中獲取變量,只從工作空間范圍中獲取變量 .
下面是我能想到的唯一解決方案,它基本上將傳入的向量轉換為字符串,然后創建一個在模型啟動時被調用的函數(實際上這與eval一樣糟糕) .
這是代碼:
function [ dataOut ] = run_simulink( dataIn )
% Convert data to a string (this is the part I would like to avoid)
variableInitString = sprintf('simin = %s;', mat2str(dataIn));
% we need both the name and the filename
modelName = 'programatic_simulink';
modelFileName = strcat(modelName,'.slx');
% load model (without displaying window)
load_system(modelFileName);
% Set the InitFcn to the god awful string
% this is how the dataIn actually gets into the model
set_param(modelName, 'InitFcn', variableInitString);
% run it
sim(modelName);
% explicity close without saving (0) because changing InitFcn
% counts as changing the model. Note that set_param also
% creates a .autosave file (which is deleted after close_system)
close_system(modelName, 0);
% return data from simOut that is created by simulink
dataOut = simout;
end
你運行它是這樣的: run_simulink([0 0.25 0.5 0.75; 1 2 3 4]') 矩陣的第一部分是時間向量 .
最后,這是底層的simulink文件,其工作區塊屬性打開以保證完整性 .
(如果圖像模糊,點擊放大)
如果沒有 mat2str() 和 sprintf() ,有沒有更簡潔的方法呢? sprint 行需要永遠運行,即使是大小為50k的向量也是如此 .
總結
以上是生活随笔為你收集整理的matlab from有什么用,Matlab函数使用'fromworkspace'将向量传递给simulink的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 频谱扩展 matlab,语音信号频谱扩展
- 下一篇: 我的世界服务器linux加mod,在Li