Shyamkkhadka
Shyamkkhadka

Reputation: 1468

convert struct type to matrix octave

I have a myfile.mat that contains 3000x35 size data. When I load it into a variable as :

a = load('myfile.mat')

It gives struct of size 1x1 . How can I get exact form as matrix. This is required because I need to change certain column values.

Upvotes: 0

Views: 4592

Answers (2)

Shyamkkhadka
Shyamkkhadka

Reputation: 1468

I found the solution myself. I did like this

a = load('myfile.mat')
a = a.data

So that now I can access values of a as matrix with indexing, a(1,2) like this. It is in octave. I hope it is similar in matlab.

Upvotes: 0

Dev-iL
Dev-iL

Reputation: 24159

(This answer is valid for MATLAB, I am not sure it works exactly the same way in Octave)


You have several options:

Option 1:

If the .mat file only contains one variable, you can do:

a = struct2array(load('myfile.mat'));     % MATLAB syntax
a = [struct2cell(load('myfile.mat')){:}]; % Octave syntax

Option 2:

You need to know the name of the variable at the time of saving, which is now a name of a field in this struct. Then you would access it using:

a = load('myfile.mat'); a = a.data;

Option 3 (unrecommended!):

Just remove the a = part of the expression,

load('myfile.mat');

Then the variables inside this file will "magically spawn" in your workspace. This is not recommended because it makes it difficult (impossible?) to see when certain variables are created.

Upvotes: 6

Related Questions