tags:

views:

715

answers:

6

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!

+1  A: 

If your "input" file is an m-file, just use the name of the file in your "main" m-file. For example you might have a file called input.m that looks like this:

% File: inputs.m
a = 5;
b = 6;
c = 7;

Then, you can use it in the file main.m like this:

% File: main.m
inputs;
S = a + b + c;
Scottie T
+5  A: 

This is typically not good practice in MATLAB. The file containing the input variables would, in your example, be a script. As would your main file. MATLAB does not error when running one script from another, as suggested by ScottieT812, but under certain circumstances strange errors can arise. (Run time compiling has difficulty, variable name collisions across scripts)

A better option is to turn the inputs script into a function which returns the variables of interest

function [a,b c] = inputs
a = 5;
b = 6;
c = 7;

Then this function can be called in the main.m script.

% main.m
[a,b,c] = inputs;
s = a+b+c;
KennyMorton
A: 

I ran into the exact problem KennyMorton mentioned when trying to create runtime compiled versions of MATLAB software for my work. The software uses m-files extensively for passing arguments between functions. Additionally, we create these m-files dynamically which the deployed version of MATLAB does not play nice with. Our workaround was:

  • save the parameters to a file without the .m extension
  • read and eval the contents of the file

So, to follow the OPs example, in a function we would create a text file, INP, containing our parameters. We create this file in the directory returned by the ctfroot function. Then, in MAIN, we would use the following to retrieve these parameters:

eval(char(textread(fullfile(ctfroot, INP), '%s', 'whitespace', '');
b3
+1  A: 

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.

Mr Fooz
A: 

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:

%mydata.m
a = 1;
b = 2;


%mymain.m
mydata
whos
mymain

>>mymain
Name Size Bytes Class Attributes

a 1x1 8 double
b 1x1 8 double

This also works for functions in addition to scripts

%foo.m
function foo mydata
whos
>>foo

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.

Todd
+1  A: 

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;
    S.name = '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 =

    24
Jason S