how to create a matrix in a for-loop
4 views (last 30 days)
Show older comments
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
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
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.
Accepted Answer
Elaheh
on 10 Aug 2020
2 Comments
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 .
More Answers (2)
Elaheh
on 10 Aug 2020
1 Comment
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...
Elaheh
on 10 Aug 2020
1 Comment
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];
See Also
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!