Mathieu
Mathieu

Reputation: 5766

Saving data to .mat file in MATLAB

I'm new to MATLAB, and I can't manage to make my function work in order to save my data into a .mat file.

The input:

The idea is to save only the clean data, marked as 1 in the clean matrix.

 If clean(i) is equal to 1:
      save data(:,:,i) and labels(:,i)

This is the code I've tried to implement in the saving.m file:

function saving(EEG_struct, clean, name)

subject_number = EEG_struct.subject_number;
fs = EEG_struct.sampling_rate;
chan_names = EEG_struct.channel_names;
nb_epoch = size(EEG_struct.data, 3);

for j=1:nb_epoch
    if clean(j) == 1
        % Keep the epoch and label
        data = cat(3, data, EEG_struct.data(:,:,j));
        labels = cat(2, labels, EEG_struct.labels(:,j));
    end
end

save(name, data, labels, subject_number, fs, chan_names)

As you can see, I would like to save the data as a structure with the same shape as the EEG_struct input. Moreover, I would like to use a parfor instead of a for, but it raised me an error I didn't quite get:

An UndefinedFunction error was thrown on the workers for 'data'.  This might be because the file containing 'data' is not accessible on the workers.  Use addAttachedFiles(pool, files) to specify the required files to be attached.  See the documentation for 'parallel.Pool/addAttachedFiles' for more details. Caused by: Undefined function or variable 'data'.

Thanks for the help !

Upvotes: 1

Views: 1881

Answers (2)

Aero Engy
Aero Engy

Reputation: 3608

You can use your clean variable as a logical index and parse out your data and labels at once. So there is no need for a loop.

Also the save command needs the "names" of the vars to save not the variables themselves. So I just put ' ' around each one.

function saving(EEG_struct, clean, name)

subject_number = EEG_struct.subject_number;
fs = EEG_struct.sampling_rate;
chan_names = EEG_struct.channel_names;
nb_epoch = size(EEG_struct.data, 3);

%No need for a loop at all
data  = EEG_struct.data(:,:,logical(clean));
labels = EEG_struct.labels(logical(clean));  %This is a 1xN so I removed the extra colon operator
save(name, 'data', 'labels', 'subject_number', 'fs', 'chan_names');

EDIT: Per you comment if you want to just leave everything in the structure. I gave you 2 options for how to save it.

function saving(EEG_struct, clean, name)
%Crop out ~clead data
EEG_struct.data  = EEG_struct.data(:,:,logical(clean));
EEG_struct.labels = EEG_struct.labels(logical(clean));  %This is a 1xN so I removed the extra colon operator
% Option 1
save(name, 'EEG_struct'); 
% Option2
save(name, '-struct', 'EEG_struct');

Option 1 will directly save the struct to the MAT file. So if you were to load the data back like this:

test = load(name);

test = 
        EEG_struct: [1x1 struct]

You would get your structure placed inside another structure ... which might not be ideal or require an extra line to de-nest it. On the other hand just loading the MAT file with no outputs load(name) would put EEG_struct into your current workspace. But if in a function then it sort of springs into existence without every being declared which makes code a bit harder to follow.

Option 2 uses the '-struct' option which breaks out each field automatically into separate vars in the MAT file. So loading like this:

EEG_struct = load(name);

Will put all the fields back together again. To me at least this looks cleaner when done within a function but is probably just my preference

So comment out which ever you prefer. Also, not I did not include clean in the save. You could either append it to the MAT or add it to your structure.

Upvotes: 1

etmuse
etmuse

Reputation: 505

To get a structure the same as EEG_struct but with only the data/labels corresponding with the clean variable, you can simply make a copy of the existing structure and remove the rows where clean=0

function saving(EEG_struct, clean, name)

newstruct = EEG_struct;
newstruct.data(:,:,logical(~clean)) = '';
newstruct.labels(logical(~clean)) = '';

save(name,'newstruct');

Upvotes: 1

Related Questions