0

I have a "data.m" file that contains a handful of large arrays that I do not want in my main file. For all intents and purposes, they are in the form

a1 = [1,2,3]
a2 = [3,4,5]

How can I access a1 and a2 from another script? Or should I be putting these in a .mat file? If so, how do I do that?

1
  • What do you mean you don't want them in your main file? Do you mean they are explicitly type out in data.m? Do you mean you want them in your workspace but you don't want the code that creates them in your script? If the first case, then use a mat file, just read the docs of save. If the second case then you should be creating them in a function. Commented Mar 4, 2016 at 6:43

3 Answers 3

2

Heres an easy way:

Inside data.m, output your arrays:

function [a1, a2] = data( )
...
end

You can access these arrays from your "main" function (e.g thefunc.m) like this:

function [ ] = thefunc( )
//say you want to store array a1 into a variable X, and array a2 into variable Y
[X, ~] = data;
[~, Y] = data;
end

Of course, thefunc.m and data.m should be in the same working directory.

Sign up to request clarification or add additional context in comments.

5 Comments

What goes inside the first script? a1 = [1,2,3]; a2 = [3,4,5]? I did that and got an error "Too many output arguments."
Yes, you could put a1 = [1,2,3]; a2 = [3,4,5] inside data.m. I tried it and it works. Did the error say at which line of the code it occured at?
Could it possibly be because my arrays are 2d?
Even if a1 and a2 are 2d arrays, it will work too. Are you sure you do not have a syntax error? Maybe a missing , or something
I got it fixed now, but this was what I was doing before: I said (for example), b1 = [1,2,3], b2 = [3,4,5], [a1,a2] = [b1,b2]
1

If the values of the variables are constant, it would be better to store these in MAT files. Also, try to use functions instead of scripts unless it is necessary to use scripts. Scripts define global variables which could lead to inadvertently overwriting variables among many other issues.

Comments

0

Say we have data.m of Yours and a function foo.m that is about to use variables from there.

Turn the data.m into this form:

function[]=data()        % defines a function with no output nor input
a1=[1 2 3];
b2='string';
%Your definitions and other code

save('DataFile.mat');      % save EVERY varible used in the code with it's name

Then in foo.m

function[]=foo()
load('DataFile.mat');      % Load all variables saved in DataFile.mat

disp(b2);

You can also prevent temporary variables to be saved with the mandatory ones by deleting them just before save command by clear temp1 temp2
If you want to save some variables, say a1 in one file and other in different file You can use save('DataFile.mat','a1')

If you want to load specific variables You can use load('DataFile.mat','b1')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.