how to create a matrix in a for-loop

4 views (last 30 days)
Elaheh
Elaheh on 10 Aug 2020
Commented: Elaheh on 11 Aug 2020
Hello all.
I have 20 participants. After processing their data, I got a table of 1* a number ranging from ... to ... , for each. The unumber of columns is the same number of elecrodes in an EEG file. How can I make one matrix for all participants and then wirte all the data in one excel file (one excel file with the same number of rows as the number of particiopants)? I have written the following. the size of the "outputtable" is different for each person, ranging from ... to 62. Is it possible to write each number under the same "chLabels"?
I thank you in advance.
output_20_participants=[ ] ;
for i=1:20
fileName= soemthing
if exist (fileName)
do this
% Prepare output table
outputTable = array2table(ERP, 'VariableNames', chLabels, 'RowNames', strcat('S',convertCharsToStrings(num2str(i)))); % "ouputTable" is 1*65 table
???
end
end
writetable( output_20_participants, 'test.xlsx','WriteVariableNames', true, 'WriteRowNames', true); %I do not understand the use of "true".!!!
  2 Comments
Walter Roberson
Walter Roberson on 10 Aug 2020
true is a function call that returns logical(1); it contrasts with false which is a function call that returns logical(0) .
MATLAB generates logical(1) (also known as true) as the result of any relational test that is satisfied, and generates logical(0) (also known as false) as the result of any relational test that is not satisfied.
Using true in that context indicates that you do want the appropriate option to be active -- that you do want variable names to be written to the file, and you do want row names to be written to the file.
Walter Roberson
Walter Roberson on 10 Aug 2020
one excel file with the same number of rows as the number of particiopants
the size of the "outputtable" is different for each person, ranging from ... to 62
The unumber of columns is the same number of elecrodes in an EEG file
The size of the output table is the same as the number of electrodes in the file, each one labeled according to chLabels .
You are telling us that the size of the table is different for each person. That tells us that the number of electrodes must be different for each person. With that being the case, we would have to create a combined list of all of the electrode names known between all of the participants, and we would have to be careful to put the input data under the correct electrode name, and we would have to put in some kind of missing-data indicator (such as NaN) for each participant that did not have any information for that electrode name.
But what is the data for each participant? It seems unlikely to me that you would just have one scalar value per electrode for each participant. It seems more likely to me that you would have a row or column of data for each electrode for each participant, and that when you talk about 1 * 65 table, that each of your entries is a cell of data.
If I am correct, then that kind of data can be represented as a table() object, but it cannot be represented as an excel sheet -- not unless the total number of different electrodes (between all participants) multiplied by the longest vector of numbers, is less than 16384, the maximum number of columns in an excel sheet. And although that arrangement is technically possible, I do not recommend it at all.

Sign in to comment.

Accepted Answer

Elaheh
Elaheh on 10 Aug 2020
Thank you so much. This partly works. There is one problem though. We should have "list_of_all_electrodes" constant for all participants in the same order becuase in the "table_for_all_files", I need to have one specific electrode in one column for all people. For example, O1 in column 1. Some participants have an ERP value for it, while others have NaN.
This script putS the "nonexistent electrodes" at the end of each file.
I appreciate your time and help.
Zahra
  2 Comments
Walter Roberson
Walter Roberson on 10 Aug 2020
After you build table_for_all_files, you can do
table_for_all_files = table_for_all_files(:, list_of_all_electrodes);
and that will reorder the table to be in the order indicated in list_of_all_electrodes .
Elaheh
Elaheh on 11 Aug 2020
I got this error,
Unable to use a value of type 'cell' as an index.
Error in load_setfile_write_IN_excel (line 42)
table_for_all_files = table_for_all_files(:, list_of_all_electrodes);
it seems list of all electordes is a 63*1 cell.
Thank you

Sign in to comment.

More Answers (2)

Elaheh
Elaheh on 10 Aug 2020
Thank you very much for your detailed resopnse.
Using true in that context indicates that you do want the appropriate option to be active -- that you do want variable names to be written to the file, and you do want row names to be written to the file.
I do not know a lot about logical relations. So I do not know if this line is correct. A freind recommended this line. How should I change that?
list of all of the electrode names:
chLabels = {EEG.chanlocs.labels} brings a complete list of channels: {'FP1'} {'FPZ'} {'FP2'} {'AF3'} {'AF4'} {'F7'} {'F5'} {'F3'} {'F1'} {'FZ'} {'F2'} {'F4'} {'F6'} {'F8'} {'FT7'} {'FC5'} {'FC3'} {'FC1'} {'FCZ'} {'FC2'} {'FC4'} {'FC6'} {'FT8'} {'T7'} {'C5'} {'C3'} {'C1'} {'C2'} {'C4'} {'C6'} {'T8'} {'M1'} {'TP7'} {'CP5'} {'CP3'} {'CP1'} {'CPZ'} {'CP2'} {'CP4'} {'CP6'} {'TP8'} {'M2'} {'P7'} {'P5'} {'P3'} {'P1'} {'PZ'} {'P2'} {'P4'} {'P6'} {'P8'} {'PO7'} {'PO5'} {'PO3'} {'POZ'} {'PO4'} {'PO6'} {'PO8'} {'CB1'} {'O1'} {'OZ'} {'O2'} {'CB2'} {'VEO'} {'HEO'}
You are telling us that the ..... : yes, you are correct.
But what is the data for each participant? for each electrode I have one number such as -8.0403, 0.9727, etc. So if no channels were deleted before, for person 1, I have just maximum 65 columns. If a few channels deleted, I have as many collumns as the number of electords not more. so max 65.
So the final matrix has 20 rows as the number of participants and 65 columns at most.
I appreciate your help in wriing the script.
Zahra
  1 Comment
Walter Roberson
Walter Roberson on 10 Aug 2020
In order to indicate that you do want a particular option, you need to pass in something to say, "Yes, do that for me!". What is a name for that "something" that would make you feel more confident that you understand the line?
It seems odd to me to be worried about the exact word used to indicate that you want an option to be activated, without having the background experience to know what writing variable names or row names would mean...

Sign in to comment.


Elaheh
Elaheh on 10 Aug 2020
Thank you for your reply. I did not write how the output was produced becuase I did not want to make more confusion.
This is the story. I have been using EEG data, and I used a program to convert that data into a table for each person. This table includes ERP, which is brain's electrical potentials, AND chLables (lable of electords being max 65). I have one array for each person (outputTable). Now I want to make one martix out of these arraies and write the matrix in an excel file. The final excel file has the same number of rows as participants and the same number of columns as the electords, the max of which is 65. How should I pile up or accumulate the data from participants?
Please see the attached document.
Thank you in advance
Zahra
  1 Comment
Walter Roberson
Walter Roberson on 10 Aug 2020
If you have two table() objects that have the same set of variable names, just perhaps not in the same order, then if you vertcat() them together, then MATLAB will match up the variable names and put the data together properly.
So if for any one file
unused_electrodes = setdiff(list_of_all_electrodes, list_of_electrodes_being_used);
NA_table = array2table(nan(1,length(unused_electrodes)), 'variablenames', unused_electrodes);
table_for_this_file = [table_for_this_file, NA_table];
table_for_all_files = [table_for_all_files; table_for_this_file];

Sign in to comment.

Categories

Find more on EEG/MEG/ECoG in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!