0

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.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
Shyamkkhadka
  • 1,438
  • 4
  • 19
  • 29

2 Answers2

6

(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.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • For option 1, it gives me error as "error: 'struct2array'" in octave. May be it works for matlab, but not in octave. – Shyamkkhadka Mar 23 '17 at 10:19
  • 1
    @Shyamkkhadka I have added the equivalent Octave syntax. Give it a try! – Dev-iL Mar 23 '17 at 10:28
  • interesting, is struct2array documented anywhere for matlab? in 2016b if I type 'help struct2array' i get a single line: "struct2array Convert structure with doubles to an array." but I can't find anything further, so see also's for struct2cell, and searching the latest documentation on mathworks.com returns "No results for struct2array" – Nick J Mar 23 '17 at 22:11
  • @NickJ the function consists of 3 lines... Just open its .m file and you'll see. :) – Dev-iL Mar 23 '17 at 22:53
  • I do a bit of code contribution to octave, so I'll pass on peeking under the hood. :) – Nick J Mar 23 '17 at 23:04
0

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.

Shyamkkhadka
  • 1,438
  • 4
  • 19
  • 29