Thread Subject: importing variables from another m-file into a mfile

Subject: importing variables from another m-file into a mfile

From: Dilan

Date: 21 Aug, 2008 15:14:01

Message: 1 of 6

Hey guys,

im trying to import data from an mfile (lets say its called
example.m) this is what the m-file looks like.
clear all;
format long;

x = [-5;
   3;
  0;
   0.4;
  11;
   9;
   3;
  13;
  69;
   5.58];

y = [.907;
   0.663;
   0.87672;
   0.79;
   0.5231;
   0.543;
   0.645;
   0.506;
   0.901;
   0.602];
Now I would like to use these values of x and yvariables to
be loaded into another seperate m-file in the same
directory, how would I go about doing that.

Subject: importing variables from another m-file into a mfile

From: Walter Roberson

Date: 21 Aug, 2008 16:55:03

Message: 2 of 6

Dilan wrote:

> im trying to import data from an mfile (lets say its called
> example.m) this is what the m-file looks like.
> clear all;
> format long;
>
> x = [-5;
> 3;
> 0;
> 0.4;
> 11;
> 9;
> 3;
> 13;
> 69;
> 5.58];
>
> y = [.907;
> 0.663;
> 0.87672;
> 0.79;
> 0.5231;
> 0.543;
> 0.645;
> 0.506;
> 0.901;
> 0.602];
> Now I would like to use these values of x and yvariables to
> be loaded into another seperate m-file in the same
> directory, how would I go about doing that.

First, remove the 'clear all'.

Then, in the other m file that wants to import the values, give the
name of the exporting file as if it were a command. For example
if you had importvalues.m that needed the values, then in importvalues.m
you would code a line that just said

  example;

'example' being the name of the other file without its .m extension.

This is an example of using a "script"; you can find more about scripts
in the Matlab help.
--
Q = quotation(rand);
if isempty(Q); error('Quotation server filesystem problems')
else sprintf('%s',Q), end

Subject: importing variables from another m-file into a mfile

From: Dilan

Date: 21 Aug, 2008 23:50:04

Message: 3 of 6

Thanks Walter!!!

Just have one little problem. In the function I am creating
I would like the user to be able to input the file name:

e.g.

function quad(a,b,n,filename)

filename;
z=x+b;
l=(y+a)/n

so the user can just input whatever filename they used to
store the variables.

e.g. x and y is stored in test.m
then in matlab they would enter:
quad(3,4,1,test)


I tried the above but I get:
??? Attempt to execute SCRIPT qwe as a function.

Subject: importing variables from another m-file into a mfile

From: Walter Roberson

Date: 22 Aug, 2008 00:33:07

Message: 4 of 6

Dilan wrote:

> Just have one little problem. In the function I am creating
> I would like the user to be able to input the file name:

> function quad(a,b,n,filename)

> filename;

Make that

run(filename);

> z=x+b;
> l=(y+a)/n
>
> so the user can just input whatever filename they used to
> store the variables.
>
> e.g. x and y is stored in test.m
> then in matlab they would enter:
> quad(3,4,1,test)

Make that

quad(3,4,1,'test')
 


--
Q = quotation(rand);
if isempty(Q); error('Quotation server filesystem problems')
else sprintf('%s',Q), end

Subject: importing variables from another m-file into a mfile

From: Dilan

Date: 22 Aug, 2008 00:35:05

Message: 5 of 6

That should say
??? Attempt to execute SCRIPT test as a function.


sorry!

Subject: importing variables from another m-file into a mfile

From: RexGermania

Date: 1 Sep, 2008 11:38:19

Message: 6 of 6

Dilan schrieb:
> Hey guys,
>
> im trying to import data from an mfile (lets say its called
> example.m) this is what the m-file looks like.
> clear all;
> format long;
>
> x = [-5;
> 3;
> 0;
> 0.4;
> 11;
> 9;
> 3;
> 13;
> 69;
> 5.58];
>
> y = [.907;
> 0.663;
> 0.87672;
> 0.79;
> 0.5231;
> 0.543;
> 0.645;
> 0.506;
> 0.901;
> 0.602];
> Now I would like to use these values of x and yvariables to
> be loaded into another seperate m-file in the same
> directory, how would I go about doing that.
>
>
>
>
Hi Dilan,
I've done something like that using a function, wherein I placed my
variables in a structure. Later on, I call this function to get the
structure, which contains the variables. In your case:

function myvars = example

%create the structure
myvars = struct;

x = [-5; 3; 0; 0.4;... 5.58];

y = [.907; 0.663; 0.87672;... 0.602];

% fill the structure

myvars.x = x;
myvars.y = y;

~~~~~~~endfun~~~

Now in your other file, just call the function and extract your variables

myvars = example;

x = myvars.x;
y = myvars.y;

good luck
RexGermania

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com