Reading input m-file in a main m-file

Tags: matlab

Hallo, I have a question about MATLAB I am not experienced in Matlab and I would like to tell me if i have an input file (m-file) that contains some variables with their numbers ie a=5,b=6,c=7 and i want to use that m file in another program(main m-file) that uses these variables to calculate S=a+b+c. How in the main file i can read the input file?What commands should I use?What the first line should be? Assume the input file is called INP and the main MAIN. Thank you!


For this sort of stuff (parameters that are easily adjusted later) I almost always use structures:

function S = zark
    S.wheels = 24;
    S.mpg = 13.2; = 'magic bus';
    S.transfer_fcn = @(x) x+7;
    S.K = [1 2; -2 1];

Then you can return lots of data without having to do stuff like [a,b,c,d,e,f]=some_function;

One nice thing about structures is you can address them dynamically:

>> f = 'wheels';
>> S.(f)

ans =

By : Jason S

If the data script is just a script, you can call it from a function or another script directly. Not extra commands required. For example:

a = 1;
b = 2;


Name Size Bytes Class Attributes

a 1x1 8 double
b 1x1 8 double

This also works for functions in addition to scripts

function foo mydata

Name Size Bytes Class Attributes

a 1x1 8 double
b 1x1 8 double

Generally, it is preferable to use a MAT or other data file for this sort of thing.

By : Todd

It sounds like you want to have some global configuration information that's used by scripts. Often, it's much better to create functions and pass values as arguments, but sometimes it makes sense to do things the way you suggest. One way to accomplish this is to save the information in a file. See "load" and "save" in the Matlab documentation.

By : Mr Fooz

This video can help you solving your question :)
By: admin