An EEG dataset .txt , I want to transpose the rows and columns for all files.

I have an EEG dataset .txt files (55 files) , each file contains 3 columns and hundreds of rows. I want to change all .txt files to 3 rows and hundreds columns (transpose ).

2 Comments

It is easier in MATLAB to keep them as they are now. I would not change them.
Actually I'm new to MATLAB and I have a running code that deals with EEG files such as rows are channels. that's way I want to change them.

Sign in to comment.

Answers (2)

I would still keep them as they are in the files, and transpose them after you read the files.
EEG = readmatrix('S9 HC.txt') % Original
EEG = 18204×3
1703509 1326511 1528584 1711498 1334520 1536615 1705921 1328796 1531118 1694223 1317011 1519531 1692689 1315472 1517843 1703451 1326439 1528569 1711428 1334467 1536607 1705938 1328835 1531153 1694351 1317151 1519643 1692668 1315488 1517863
EEGT = EEG.' % Transposed
EEGT = 3×18204
1703509 1711498 1705921 1694223 1692689 1703451 1711428 1705938 1694351 1692668 1703313 1711312 1705773 1694428 1692821 1703274 1710918 1705693 1694156 1692196 1702908 1711174 1706023 1694625 1692823 1703394 1711520 1706029 1694092 1692186 1326511 1334520 1328796 1317011 1315472 1326439 1334467 1328835 1317151 1315488 1326314 1334344 1328729 1317221 1315551 1326341 1334171 1328823 1317164 1315211 1326062 1334344 1329054 1317561 1315857 1326618 1334725 1329054 1316944 1315122 1528584 1536615 1531118 1519531 1517843 1528569 1536607 1531153 1519643 1517863 1528429 1536389 1530985 1519640 1517866 1528451 1536228 1531088 1519478 1517266 1527826 1536129 1531097 1519718 1517921 1528464 1536523 1531081 1519127 1517216
You can also use the transpose, .' function for this.
In MATLAB there are two kinds of matrix transposition, ‘normal’ transposition (.') and complex conjugate transposition provided by the ctranspose, ' function ('). For real numbers, these are equivalent, however not for complex numbers. For N-dimensional matrices, there is also pagetranspose, that does a non-conjugate transposition, and for good measure, I will introduce you to the permute function.
.

9 Comments

oh great thank you , but how can I do this for all the files (50 files ) ? shoud I use loop or what ? can you give me the syntacs .. lets say I save them in the "EEGData" folder.
(EEG = readmatrix('S9 HC.txt')
EEGT = EEG.' )
Thank you for helping.
Just do it in your code for each file you import, just after importing it. Leave the files themselves as they are.
I am sorry I'm confused about where to add these lines..
I am working on a classification problem, I named the .txt files either H or N, H files belonging to the yes group while N files belong to no group. so I used this script to read the files and then separate them into an approperate group. then I save them as .mat file (which contains 2 groups, each group containing its files). Later on, I will segment the EEG and process it .. so where should I write these lines?
pathM = [pathd, '/MAT Files/'];
pathd = [pathd, '/EEG_3/'];
YES = {};
NO = {};
samples = dir(fullfile(pathd, '*.txt'));
samples = {samples.name};
samples = samples';
numSamps = size(samples, 1);
numYES = 1;
numNO = 1;
for i = 1:numSamps
samp = char(samples(i,:));
type = samp(end-7:end-4);
state = samp(end-4:end-3);
if state == 'H'
YES{numYES,1} = samp;
numYES = numYES + 1;
elseif state == 'N'
NO{numNO, 1} = samp;
numNO = numNO + 1;
end
end
save([pathM, 'EEG_3channels', '.mat'], 'YES', 'NO');
Thank you..
I do not understand what you are doing, however it does not appear to be correct.
You need to import the files, probably using readmatrix or a similar function, before you can do anyting with their contents. I do not see that anywhere.
With respect to processing the files, see Import or Export a Sequence of Files for guidance.
.
Thank you for your time.
But this code is correct, I used it in the same problem but with a different dataset ( .edf files) and it works with good results. My problems now are:
- The new dataset is .txt which I'm not sure how to read.
- The channels are the columns, not rows (as .edf) files.
Again, I'm new to Matlab, so maybe these seem to be easy to work with but currently, I am stuck in these problems.
Thank you.
I still do not understand where in your code you are importing the data in the files.
Could you upload an example file so that I can see how your code works?
I'll try to explain what the code dose:
I have binart clasification problem ( yes , no ). the data is labled by the file name if the name contains H so it belongs to group "yes". and if the name contains N so it belongs to group "no".
first I create the datafiles by accessing the dataset folder and seperate the files by names into belonging group and save this in( DATA.mat) file.
afterthat, I want to prepare the data for training and testing and segment the data.
whe I want to do step 2, I rty to read the .txt files in ( DATA.mat ) file.
Here I share segment of the code that works with .edf files and reads the files ( the underlined lines) then segment them:
function [DataX, DataY ] = SegmentEEGIntoInstances (file, label, PLength, stride, channels)
[data, header] = edfread(file{1,1});
EEG = header(channels, :);
EEGLength = size(EEG,2);
numInstances = 1+floor((EEGLength- PLength)/stride);
..... etc
My problem here how can I read with .txt datasets and change the rows to be the channels so I can segment the data and train the model.
kindly, check your inbox.
Please upload a representative EDF file here. If that is not a valid file type, use the zip function to enclose it in a .zip file and then upload that.
I will also need the entire code of the ‘SegmentEEGIntoInstances’ function so I can see what you are doing with the file.
(I do not do anything offline. I keep everything here so it all makes sense, if anyone else has similar problems and needs to see this solution.)

Sign in to comment.

Hi!
You can try a for loop, however there is a better way to do this is to use tabularTextDatastore(location) and then use readall function to read all the file, later on you can transpose and do whatever you want with the readall function output.
Let me know if this helps.
Abderrahim

Categories

Asked:

on 3 Sep 2023

Commented:

on 10 Sep 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!