How can i write input variables from a text file into my script

7 次查看(过去 30 天)
The text file contaions the following variables. I have tried various ways, which are mentioned before but they don't seem to work. I would appriciate if someone could help. Thank you!
n = 3;
m = [120 30 30]';
k = [6000 1500 1500]';
d = [150 0 0]';
m_con = {[1,2,3],2,3};
md_con = {[1,2,3],2,3};
k_con = {1;[1,2];[1,3]};
d_con = {1;[1,2];[1,3]};
q0=[1 -1 0.5];
v0=[0 0 0];
ti = 0;
tf = 5;

回答(1 个)

Rik
Rik 2021-1-11
It looks like you can simply use run to run this code as a script.
I would encourage you to rethink this strategy for loading parameters, as running a user-input text file is asking for trouble.
  4 个评论
Rik
Rik 2021-1-11
This is the general idea I use in many FEX submissions. If you need a more fleshed out example (or want to see how you could extend this to handle Name,Value pairs) I encourage you to have a look there.
function z=MyFunction(x,y,options)
%one line description goes here
%
%general description goes here
%
%description of syntax options goes here
%create a struct that contains the defaults
persistent default_options
if isempty(default_options)
default_options=struct;
default_options.param1=1;
default_options.param2=10;
end
%loop through the struct to replace the default parameters by the inputs
opts=default_options;fn=fieldnames(default_options);
for n=1:numel(fn)
if isfield(options,fn{n})
opts.(fn{n})=options.(fn{n});
end
end
%%% actual code goes here %%%
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by