ERPTier ERPTier_extractEventsWhereColumn_number (ERPTier me, Table table, long columnNumber, int which_Melder_NUMBER, double criterion) { try { Table_checkSpecifiedColumnNumberWithinRange (table, columnNumber); Table_numericize_Assert (table, columnNumber); // extraction should work even if cells are not defined if (my events -> size != table -> rows -> size) Melder_throw (me, " & ", table, ": the number of rows in the table (", table -> rows -> size, ") doesn't match the number of events (", my events -> size, ")."); autoERPTier thee = Thing_new (ERPTier); Function_init (thee.peek(), my xmin, my xmax); thy numberOfChannels = my numberOfChannels; thy channelNames = NUMvector <wchar_t *> (1, thy numberOfChannels); for (long ichan = 1; ichan <= thy numberOfChannels; ichan ++) { thy channelNames [ichan] = Melder_wcsdup (my channelNames [ichan]); } thy events = SortedSetOfDouble_create (); for (long ievent = 1; ievent <= my events -> size; ievent ++) { ERPPoint oldEvent = my event (ievent); TableRow row = table -> row (ievent); if (Melder_numberMatchesCriterion (row -> cells [columnNumber]. number, which_Melder_NUMBER, criterion)) { autoERPPoint newEvent = Data_copy (oldEvent); Collection_addItem (thy events, newEvent.transfer()); } } if (thy events -> size == 0) { Melder_warning ("No event matches criterion."); } return thee.transfer(); } catch (MelderError) { Melder_throw (me, ": events not extracted."); } }
autoMatrix Table_to_Matrix (Table me) { try { autoMatrix thee = Matrix_createSimple (my rows.size, my numberOfColumns); for (long icol = 1; icol <= my numberOfColumns; icol ++) { Table_numericize_Assert (me, icol); } for (long irow = 1; irow <= my rows.size; irow ++) { TableRow row = my rows.at [irow]; for (long icol = 1; icol <= my numberOfColumns; icol ++) { thy z [irow] [icol] = row -> cells [icol]. number; } } return thee; } catch (MelderError) { Melder_throw (me, U": not converted to Matrix."); } }
autoStringsIndex Table_to_StringsIndex_column (Table me, long column) { try { if (column < 1 || column > my numberOfColumns) { Melder_throw (U"Invalid column number."); } long numberOfRows = my rows -> size; Table_numericize_Assert (me, column); autoNUMvector<char32 *> groupLabels (1, numberOfRows); for (long irow = 1; irow <= numberOfRows; irow++) { groupLabels[irow] = ((TableRow) my rows -> item [irow]) -> cells [column] .string; } autoStrings thee = strings_to_Strings (groupLabels.peek(), 1, numberOfRows); autoStringsIndex him = Strings_to_StringsIndex (thee.peek()); return him; } catch (MelderError) { Melder_throw (me, U"No StringsIndex created from column ", column, U"."); } }
TableOfReal Table_to_TableOfReal (Table me, long labelColumn) { try { if (labelColumn < 1 || labelColumn > my numberOfColumns) labelColumn = 0; autoTableOfReal thee = TableOfReal_create (my rows -> size, labelColumn ? my numberOfColumns - 1 : my numberOfColumns); for (long icol = 1; icol <= my numberOfColumns; icol ++) { Table_numericize_Assert (me, icol); } if (labelColumn) { for (long icol = 1; icol < labelColumn; icol ++) { TableOfReal_setColumnLabel (thee.peek(), icol, my columnHeaders [icol]. label); } for (long icol = labelColumn + 1; icol <= my numberOfColumns; icol ++) { TableOfReal_setColumnLabel (thee.peek(), icol - 1, my columnHeaders [icol]. label); } for (long irow = 1; irow <= my rows -> size; irow ++) { TableRow row = static_cast <TableRow> (my rows -> item [irow]); wchar_t *string = row -> cells [labelColumn]. string; TableOfReal_setRowLabel (thee.peek(), irow, string ? string : L""); for (long icol = 1; icol < labelColumn; icol ++) { thy data [irow] [icol] = row -> cells [icol]. number; // Optimization. //thy data [irow] [icol] = Table_getNumericValue_Assert (me, irow, icol); } for (long icol = labelColumn + 1; icol <= my numberOfColumns; icol ++) { thy data [irow] [icol - 1] = row -> cells [icol]. number; // Optimization. //thy data [irow] [icol - 1] = Table_getNumericValue_Assert (me, irow, icol); } } } else { for (long icol = 1; icol <= my numberOfColumns; icol ++) { TableOfReal_setColumnLabel (thee.peek(), icol, my columnHeaders [icol]. label); } for (long irow = 1; irow <= my rows -> size; irow ++) { TableRow row = static_cast <TableRow> (my rows -> item [irow]); for (long icol = 1; icol <= my numberOfColumns; icol ++) { thy data [irow] [icol] = row -> cells [icol]. number; // Optimization. //thy data [irow] [icol] = Table_getNumericValue_Assert (me, irow, icol); } } } return thee.transfer(); } catch (MelderError) { Melder_throw (me, ": not converted to TableOfReal."); } }