简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM