【问题标题】:How to read a variable in set_param SIMULINK function?如何读取 set_param SIMULINK 函数中的变量?
【发布时间】:2019-02-24 16:53:26
【问题描述】:

我在从文件中读取值并将其放入 Set_param 函数中时遇到问题,该函数将更改 SIMULINK 模型参数。这是我的代码,在这里我从 txt 文件中获取 A 的值,但我想将 A 放入 Set_param。 当 simulink 打开时,它显示 A 而不是模型中 A 的值。

open_system('Transient.slx')  %this will open the simulink model
% get a value from txt file and put it in variable A
A= dlmread('C:\xampp\htdocs\RCE\MATLAB\FYP_expirement\SpeedControl\exp_value.txt');
% here when i put the variable A the function does not accept it
set_param('Transient/Gain','Gain','A')

我尝试不带单引号的 A 也会出错。

set_param('Transient/Gain','Gain', A)

如何在这个函数中插入一个变量?还是有其他解决方案?

【问题讨论】:

  • “不接受”是什么意思?确切的错误信息是什么?您的第一个语法看起来是正确的。
  • 它没有报错,但是在模块中Gain的值将是一个错误的字符A。我想要从文件中获取的 A 的数值。只有当我不放单引号时才会出错。

标签: matlab simulink


【解决方案1】:

非常感谢你,我找到了让它工作的方法。似乎 set_param 只接受字符。所以在得到值后我应该把它转换成这样的字符串:

A= dlmread('C:\xampp\htdocs\RCE\MATLAB\FYP_expirement\SpeedControl\exp_value.txt');
s = num2str(A)
set_param('Transient/Gain','Gain', s)

然后当我在函数中插入 s 时,我不必使用引号。

【讨论】:

  • 标准做法是在块中包含字符串 A,根据您问题中的代码。这称为模型参数化。在模型初始化期间,Simulink 将在 MATLAB 工作区中查找变量 A 的值并使用它。
猜你喜欢
  • 1970-01-01
  • 2020-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多