user3051460
user3051460

Reputation: 1485

How to read mat file and store in a variable

I have two .mat files. And I want to read these data of two mat files and store in variables A and B. This is my code, but I think it is not good. Can you help me store it without using matArray in matlab? (Varibale matArray is born when you call load function)

    load input1.mat;
    A=matArray;
    load input2.mat;
    B=matArray

Thank you so much

Upvotes: 1

Views: 381

Answers (2)

Peter
Peter

Reputation: 14947

Use an output argument with the load function.

A = load('input1.mat');
B = load('input2.mat');

The two arrays will now be fields of the structures A and B:

size(A.matArray);
plot(B.matArray);

If you choose to copy these into simpler variables, or stick with your current copying approach, you should know that the copy operation is extremely efficient. When you do A = matArray; A shares the data of matArray until one of them is modified. Therefore, if you delete matArray before modifying A, no extra memory is consumed by the copy.

Upvotes: 1

Floris
Floris

Reputation: 46415

You had it right. The variable name that you have when you save the file is the name that will appear in the workspace when you load the file again. Best you can do:

load('input1.mat');
A=matArray;
load('input2.mat');
B=matArray;
clear matArray

At least you'll get the space back at the end. There is, to my knowledge, no "rename" function in Matlab...

Of course if you know what you want to name the variable when you read it in, you should save it as such:

A = matArray;
save('input1.mat', 'A');

etc

Upvotes: 2

Related Questions