how to edit an eeg data file? - matlab

i have a file : 30_19.1.20.eeg , which contains eeg data , i want to split this data into two halves . so first i imported the data file to matlab using the following :
i imported a 30_19.1.vhdr file into eeglab using : file->import data -> using eeg functions and plugins -> from brain vis. Rec.vhdr or ahdr file
after that i received in the matlab workplace the following :
enter image description here
so i want to open the data file so i pressed the EEG struct ,
this is what it contains :
enter image description here
and this struct contains a filed called data , when i open it i have a matrix that contains all of the data
basically i want to split this data into half .
how can i do such a thing ?
i tried to change this struct but i always get the following error :
enter image description here

Structures are variables which have "sub-variables" called properties inside them, a bit like directory / sub-directory structures.
Assuming the data in your eeg variable is called data, you can do the following to load the data into a new variable: m = eeg.data. If you want to load half of that data, assuming the half is in terms of numbers of rows, then you could write:
m = eeg.data(1:floor(size(eeg.data,1)/2),:);

You may want to "select data" from the continuous EEG dataset file. There are 2 options:
time range in seconds
point range (one-sample precision)
You have to apply this procedure twice (lower range, higher range) to get the 2 separate files.
After doing so, you can type EEG.history in the command window to get the Matlab code that has been executed.

Related

pixelLabelDatastore from loaded image in workspace

I have multiple small *.mat files, each containing 4 input images (template{1:4} and a second channel template2{1:4}) and 4 output images (region_of_interests{1:4}), a binarized ('mask') image to train a deep neural network.
I basically followed an example on Mathworks and it suggests to use a function (in this example #matreader) to read in custom file formats.
However ...
It seems impossible to load multiple images from one *.mat file using any load function as it only allows one output, and imageDatastore doen't seem to allow loading data from workspace. How could this be achieved?
Similarly, it seems impossible to load a pixelLabelDatastore from a workspace variable. As a workaround I ended up saving the contents of my *.mat file to an image (using imwrite, saving to save_dir), and re-loading it from there (in this case, the function doesn't even allow to load *.mat files.). (How) can this be achieved without re-saving the file as image?
Here my failed attempt to do so:
%main script
image_dir = pwd; %location of *.mat files
save_dir = [pwd '/a/']; %location of saved output masks
imds = imageDatastore(image_dir,'FileExtensions','.mat','ReadFcn',#matreader); %load template (input) images
pxds = pixelLabelDatastore(save_dir,{'nothing','something'},[0 255]);%load region_of_interests (output) image
%etc, etc, go on to train network
%matreader function, save as separate file
function data=matreader(filename)
in=1; %give up the 3 other images stored in template{1:4}
load(filename); %loads template and template2, containing 4x input images each
data=cat(3,template{in},template2{in}); %concatinate 2 template input images in 3rd dimension
end
%generate example data for this question, will save into a file 'example.mat' in workspace
for ind=1:4
template{ind}=rand([200,400]);
template2{ind}=rand([200,400]);
region_of_interests{ind}=rand([200,400])>.5;
end
save('example','template','template2','output')
You should be able to achieve this using the standard load and save function. Have a look at this code:
image_dir = pwd;
save_dir = pwd;
imds = imageDatastore(image_dir,'FileExtensions',{'.jpg','.tif'});
pxds = pixelLabelDatastore(save_dir,{'nothing','something'},[0 255]);
save('images.mat','imds', 'pxds')
clear
load('images.mat') % gives you the variable "imds" and "pxds" directly -> might override previous variables
tmp = load('images.mat'); % saves all variables in a struct, access it via tmp.imds and tmp.pxds
If you only want to select the variables you want to load use:
load('images.mat','imds') % loads "imds" variable
load('images.mat','pxds') % loads "pxds" variable
load('images.mat','imds','pxds') % loads both variables
EDIT
Now I get the problem, but I fear this is not how it is going to work. The Idea behind the Datastore objects is, that it is used if the data is too big to fit in memory as a whole, but every little piece is small enough to fit in memory. You can use the Datastore object than to easily process and read multiple files on a disk.
This means for you: Simply save your images not as one big *mat file but as multiple small *.mat files that only contain one image.
EDIT 2
Is it strictly necessary to use an imageDatastore for this task? If not you can use something like the following:
image_dir = pwd;
matFiles = dir([image_dir '*.mat']);
for i=1:length(matFiles)
data = load(matFiles(i).name);
img = convertMatToImage(data); % write custom function which converts the mat input to your image
% or something like this:
% for j=1:4
% img(:,:,j) = cat(3,template{j},template2{j});
% end
% process image
end
another alternative would be to create a "image" in your 'matreader' which does not only have 2 bands but to simply put all bands (all templates) on top of each other providing a "datacube" and then in an second step after iterating over all small mat files and reading them splitting the single images out of the one bigger datacube.
would look something like this:
function data=matreader(filename)
load(filename);
for in=1:4
data=cat(3,template{in},template2{in});
end
end
and in your main file, you have to simply split the data into 4 pieces.
I have never tested it but maybe it is possible to return a cell instead of a matrix?
function data=matreader(filename)
load(filename);
data = cell(1,4)
for in=1:4
data{in}=cat(3,template{in},template2{in});
end
end
Not sure if this would work.
However, the right way to go forward from here really depends on how you plan to use the images from imds and if it is really necessary to use a imageDatastore.

Creating Feature vector in .mat file for training dataset

I wanted to create a feature vector for training dataset and wanted to store all feature as rows in the.mat file. The .mat file must be in the form Feature Vector. I am able to extract Feature of 1 image and store it in excel file or .mat file but not able to extract all image feature and store it in .mat file. Can anyone knows this?
Its something like appending the same variables in the same .mat file. I have tried
save('feat.mat','feature','-append');
Where 'feature' is an array
feature = [mydata, stats{k}];
I have a folder which contains images who's feature I wanted to extract and store as training dataset. Any help will be appreciated.
the code was rectified.
stats = graycoprops(GLCM_values{'contrast','homogeneity','Correlation','Energy'});
stats was a structure so converted into the array using
features=struct2array(stats);
and was able to save the features

Simulink does not load data from .mat file

I am trying to load my data into my simulation model from .mat file in Simulink with "From file" block. I am using Matlab 2012a. My data file is one dimensional vecor of real numbers (type double) with dimensions 1x200.
When I run my simulation I am getting following error:
In Matlab preferences, I have already set the setting "Save MAT-files that can be loaded in" to option "Matlab version 7.3, or later", but this does not work. I have also tested this approach without success: http://www.mathworks.com/matlabcentral/answers/47422-simulink-does-not-support-loading-the-input-data-in-file
I would be thankful for any ideas.
As per the documentation in the From File block help (obtained by pressing the Help button on the block dialog):
Matrix format can be used only for vector, double, noncomplex signals. Each
column of the matrix must have a time stamp in the first row and a vector
containing the corresponding data sample in the subsequent rows.
Your 1x200 vector does not match this format. You need to add a first row that represents time.

MATLAB EEG signal processing - Channel location file

I'm trying the EEGLAB and FASTER plugins for MATLAB in order to do some processing for my EEG data, When trying to load the data file, I'm asked to choose the "channel location file", but I don't have that with my data, I was wondering if I can create it myself? And if so, How? I know that each channel in my data corresponds to a specific electrode, how can I write that in the location file? Thank you
You can do it by file or by code.
By file (I did not test it so it may not work):
create a text file with electrode names - one electrode per line, the order should be the same as in your file. Load the file through edit -> channel locations --> read locations (left bottom corner of the gui). Choose your text file and then use "look up locs" button to get corresponding locations on BESA or MNI head model.
By code and gui (should work well):
Create a variable with electrode names (have to be correct names in correct order):
elec_names = {'Cz', 'O1', 'O2', 'Fp1', 'Fp2'};
[EEG.chanlocs.labels] = deal(elec_names{:});
eeglab redraw;
Then use the edit -> channel locations --> look up locs option. Later you can type eegh in command window to get the command that would work on your computer.
This sounds like you are not really aware of how EEGLAB works.
From the EEGLAB wiki page on the topic of "Channel Location"
To plot EEG scalp maps in either 2-D or 3-D format, or to estimate
source locations for data components, an EEGLAB dataset must contain
information about the locations of the recording electrodes.
KEY STEP 5: Load the channel locations.
To load or edit channel location information contained in a dataset, select Edit > Channel locations.

Automatic file opening in MATLAB

I'm running a program on magnetometry. I have a file which contains over 10 text files, each containing the data (amplitude vs frequency) at a precise magnetic field value.
My program then reads each of those files, plotting the data, making a fit over those and then I use this fit to find the magnetic field depending on the distance of the frequency between 2 amplitude peaks (that's just theory, it's not necessary to understand this part).
All I want is some lines of code that would open all the files in the specified directory and let me use the datas
(i.e. data = importdata(filenames{i},delimiterIn,headerlinesIn);)
And later on I have a line that ask the user which data file he wants to open, and it gives back the magnetic field value.
So I need to use two folders : the one containing the data to create my fit and equations.
And the one containing whatever data file the user wants to open to find the magnetic field applied while getting data.
It isn't clear from the question if you want the user to do this interactively or not, so I assumed you do.
To select a folder you can use uigetdir , for example:
d = uigetdir('C:\');
will displays directories on the C: drive to select from, etc...
Similarly, to select all the files in that folder you can use dir. For example, if you want to pick out all the TXT files in a folder that the user selects:
d = uigetdir(pwd, 'Select a folder');
files = dir(fullfile(d, '*.txt'));