void loadPattern(int pattIndex) { PatternLoader p; StringMatrix pattData = p.readPattern(pattIndex); for(int i = 0; i < pattData.size(); i++) { for(int j = 0; j < pattData[i].size(); j++) { this->tracks[i].setNote(j, stoi(pattData[i][j])); } } }
void loadKit(int kitIndex) { PatternLoader p; StringMatrix kitData = p.readKit(kitIndex); for(int i = 0; i < kitData.size(); i++) { for(int j = 0; j < kitData[i].size(); j++) { this->trackSynths[i].setParameter("param" + to_string(j), stof(kitData[i][j])/238.0); } } }
bool MatlabEngine::PutCells( const string& inExp, const StringMatrix& inValue ) { int sizeDim2 = inValue.empty() ? 0 : inValue[ 0 ].size(); mxArray* mat = mxCreateCellMatrix( inValue.size(), sizeDim2 ); int indices[] = { 0, 0 }; for( size_t i = 0; i < inValue.size(); ++i ) { indices[ 0 ] = i; for( size_t j = 0; j < inValue[ i ].size(); ++j ) { indices[ 1 ] = j; int idx = mxCalcSingleSubscript( mat, 2, indices ); mxDestroyArray( mxGetCell( mat, idx ) ); mxArray* val = mxCreateString( inValue[ i ][ j ].c_str() ); mxSetCell( mat, idx, val ); } } bool success = PutMxArray( inExp, mat ); mxDestroyArray( mat ); return success; }
string Pattern::packData(StringMatrix data) { string s = ""; for(int i = 0; i < data.size(); ++i) { for(int j = 0; j < data[i].size(); ++j) { s += data[i][j] + " "; } s += "\n"; } return s; }
bool MatlabEngine::PutCells( const string& inExp, const StringMatrix& inValue ) { mwSize sizeDim2 = static_cast<mwSize>( inValue.empty() ? 0 : inValue[ 0 ].size() ); mxArray* mat = mxCreateCellMatrix_( static_cast<mwSize>( inValue.size() ), sizeDim2 ); mwIndex indices[] = { 0, 0 }; for( mwIndex i = 0; i < static_cast<mwIndex>( inValue.size() ); ++i ) { indices[ 0 ] = i; for( mwIndex j = 0; j < static_cast<mwIndex>( inValue[ i ].size() ); ++j ) { indices[ 1 ] = j; int idx = mxCalcSingleSubscript_( mat, 2, indices ); mxDestroyArray_( mxGetCell_( mat, idx ) ); mxArray* val = mxCreateString_( inValue[ i ][ j ].c_str() ); mxSetCell_( mat, idx, val ); } } bool success = PutMxArray( inExp, mat ); mxDestroyArray_( mat ); return success; }
// Helper function to write 'matrix' of strings to a CSV file. static void writeCSV(const StringMatrix& string_matrix, const std::string& filename) { CHECK_GE(string_matrix.size(), 1) << "Provided matrix of strings had no entries."; std::ofstream out_file_stream; out_file_stream.open(filename.c_str()); // Iterate over the rows of the string matrix and write comma-separated fields. for (StringMatrix::const_iterator it = string_matrix.begin(); it != string_matrix.end(); ++it) { CHECK_GE(it->size(), 1) << "String matrix row has no entries."; out_file_stream << it->at(0u); for (size_t i = 1u; i < it->size(); ++i) { out_file_stream << "," << it->at(i); } out_file_stream << std::endl; } out_file_stream.close(); }
MatlabEngine::StringMatrix MatlabEngine::GetCells( const string& inExp ) { StringMatrix result; mxArray* ans = GetMxArray( inExp ); if( ans ) { int numDims = mxGetNumberOfDimensions( ans ); const int* dims = mxGetDimensions( ans ); if( numDims != 2 ) bcierr << "Can only handle two dimensions" << endl; result.resize( dims[ 0 ], vector<string>( dims[ 1 ] ) ); int indices[] = { 0, 0 }; for( size_t i = 0; i < result.size(); ++i ) { indices[ 0 ] = i; for( size_t j = 0; j < result[ i ].size(); ++j ) { indices[ 1 ] = j; int idx = mxCalcSingleSubscript( ans, 2, indices ); mxArray* cell = mxGetCell( ans, idx ); if( cell ) { const char* s = mxArrayToString( cell ); if( s == NULL ) bcierr << "Could not read string value \"" << inExp << "\"" << endl; else { result[ i ][ j ] = s; mxFree( ( void* )s ); } } } } mxDestroyArray( ans ); } return result; }
void MatLabUdmTruthTable::setupTruthTable( void ) { State udmTruthTable = getUdmTruthTable(); udmTruthTable.Name() = udmTruthTable.name() = getUniqueName().c_str(); udmTruthTable.RefId() = getRefId(); udmTruthTable.Decomposition() = "FUNC_STATE"; udmTruthTable.Order() = "-2"; udmTruthTable.EnterAction() = RegexCommon::eliminateContinuations( MatLabEngine::globalEngine().getMxStringValue( getMatLabObject() + ".LabelString" ) ); std::string conditionTable = MatLabEngine::globalEngine().getData( getMatLabObject() + ".ConditionTable" ); StringVectorManager conditionSTM = getStringVectorManager( conditionTable ); std::string actionTable = MatLabEngine::globalEngine().getData( getMatLabObject() + ".ActionTable" ); StringVectorManager actionSTM = getStringVectorManager( actionTable ); std::string functionBody; StringMatrix conditionStringMatrix = conditionSTM.getStringMatrix(); int noConditions = (int) conditionStringMatrix[0]->size() - 2; int noConditionTerms = (int) conditionStringMatrix.size() - 1; static boost::regex crnlPattern( "\\r|\\n", boost::regex_constants::perl ); for( int ix = 0 ; ix < noConditions ; ++ix ) { if ( ix != 0 ) functionBody += "else"; functionBody += "if "; std::string condition; for( int jx = 0 ; jx < noConditionTerms ; ++jx ) { StringVector &stringVector = *conditionStringMatrix[ jx ]; std::string tf = stringVector[ ix + 2 ]; std::string rawCondition = "(" + boost::regex_replace( stringVector[1], crnlPattern, " " ) + ")"; if ( tf.find( '-' ) != std::string::npos ) continue; if ( tf.find( 'F' ) != std::string::npos ) rawCondition = "!" + rawCondition; if ( !condition.empty() ) condition += " && "; condition += rawCondition; } if ( condition.empty() ) condition = "1"; functionBody += condition + "\n"; StringVector actionVector; std::string actions = ( *conditionStringMatrix.back() )[ ix + 2 ]; static boost::regex actionPattern( "[0-9]+|[A-Za-z_][A-Za-z_0-9]*" ); boost::match_results< std::string::const_iterator > results; while ( boost::regex_search( actions, results, actionPattern ) ) { actionVector.push_back( results[0] ); actions = results.suffix(); } static boost::regex newlinePattern( "\n" ); for( StringVector::iterator stvItr = actionVector.begin() ; stvItr != actionVector.end() ; ++stvItr ) { std::string actionSpec = *stvItr; if ( isdigit( actionSpec[0] ) ) { int actionNo = boost::lexical_cast< int >( actionSpec ) - 1; std::string action = (*actionSTM.getStringMatrix()[ actionNo ])[ 1 ]; action = boost::regex_replace( action, newlinePattern, "\n " ); functionBody += " " + action + "\n"; } else { std::string action = (*actionSTM.getStringVectorMap()[ actionSpec ])[ 1 ]; action = boost::regex_replace( action, newlinePattern, "\n " ); functionBody += " " + action + "\n"; } } } functionBody += "end\n"; udmTruthTable.DuringAction() = functionBody; }