how to create a matrix in a for-loop
4 views (last 30 days)
Show older comments
Commented: Elaheh on 11 Aug 2020
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=[ ] ;
if exist (fileName)
% Prepare output table
outputTable = array2table(ERP, 'VariableNames', chLabels, 'RowNames', strcat('S',convertCharsToStrings(num2str(i)))); % "ouputTable" is 1*65 table
writetable( output_20_participants, 'test.xlsx','WriteVariableNames', true, 'WriteRowNames', true); %I do not understand the use of "true".!!!
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.
More Answers (2)
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...
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];
Find more on EEG/MEG/ECoG in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!