How to store a matrix so that I can call upon it in any other file I am working in?

Hi there,
I am trying to save a matrix of data that I want to use, so that I can call upon it whenever I wish. Plus, I don't want it taking up space in my other script I am working on. I have been searching online how to do this apparent simple task, but can't seem to get it right.
So say I'm in a file called Frame2.mat, and the matrix is:
A = rand(10,10)
A = 10×10
0.5228 0.7856 0.3229 0.6056 0.3178 0.8621 0.8351 0.8522 0.6084 0.0708 0.8254 0.4739 0.8760 0.3915 0.5663 0.8099 0.1573 0.9719 0.3930 0.7211 0.9450 0.8130 0.5576 0.2800 0.4407 0.3070 0.9011 0.7068 0.2419 0.1356 0.0966 0.8290 0.4090 0.8699 0.8585 0.7259 0.0443 0.7975 0.6078 0.6212 0.0465 0.6980 0.5365 0.0164 0.0368 0.4520 0.2349 0.6107 0.2868 0.7108 0.1046 0.3271 0.5281 0.3637 0.6896 0.2049 0.8954 0.0560 0.8737 0.0928 0.0100 0.8795 0.2607 0.7931 0.8346 0.3888 0.2983 0.3102 0.1165 0.0741 0.6809 0.5309 0.9333 0.4969 0.9272 0.2590 0.7933 0.3678 0.9152 0.6260 0.6133 0.5114 0.2475 0.7920 0.0814 0.5666 0.4892 0.2776 0.3486 0.4589 0.8599 0.2761 0.4248 0.8110 0.2638 0.9842 0.6746 0.2426 0.4833 0.7074
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Then I go to another file I am working in and want to call upon matrix A and extract the first column.
So in this different file (not Frame2) I want to use:
T = A(:,1)
T = 10×1
0.5228 0.8254 0.9450 0.0966 0.0465 0.1046 0.0100 0.6809 0.6133 0.8599
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
How do I do this?
Many thanks
Scott

1 Comment

"So say I'm in a file called Frame2.mat, and the matrix is: A = rand(10,10)"
Are you attempting to write a .mat file yourself as a text file? That does not make sense.

Sign in to comment.

 Accepted Answer

Declare the matrix as "global" or pass it as input argument to all functions where it is needed.
Or save it as a .mat-file and load it where it is needed.

8 Comments

So many ways. Another is to stuff it into an m-file function, that will return the desired matrix as its argument.
I have tried saving it as:
save('Frame2.mat','A')
Then
load('Frame2.mat')
But this is working.
You mean "But this is not working" ?
What error message do you get ?
Can you find a file with name "Frame2.mat" in your working directory after saving ?
I just tried it again. This time using a file called "moment_frame2" and this happened after I tried to save my matrix.
Works without problems for me:
A = [1 2;3 4];
save("Matrix.mat","A");
B = load("Matrix.mat","A").A;
B
B = 2×2
1 2 3 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Do not save your variable as a .m file, save it as a .mat file.
Or if you really want, save it as a .csv or .txt file that you then readmatrix at need, if you need the file to be editable.
You could also consider
function A = loadA()
persistent A
if isempty(A)
A = load('Matrix.mat', 'A').A;
end
end
after which you would call
A = loadA();
This had the advantage of only reading in A once and remembering the cached value the second and later times.

Sign in to comment.

More Answers (0)

Asked:

on 24 Jul 2025

Commented:

on 25 Jul 2025

Community Treasure Hunt

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

Start Hunting!