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

4 views (last 30 days)
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;

Answers (1)

Rik
Rik on 11 Jan 2021
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 Comments
Rik
Rik on 11 Jan 2021
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

Sign in to comment.

Categories

Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!