int
OperatorUtils::SaveMatrix( const QString& inFileName, const Param& inParam )
{
  if( inFileName.isEmpty() )
    return CouldNotWrite;

  bool saveAsMatrix = ( QFileInfo( inFileName ).suffix() == MATRIX_EXTENSION );
  if( !saveAsMatrix )
  {
    bool isNested = false;
    for( int row = 0; row < inParam.NumRows(); ++row )
      for( int col = 0; col < inParam.NumColumns(); ++col )
        isNested = isNested | ( inParam.Value( row, col ).Kind() != Param::ParamValue::Single );

    if( isNested )
      return CannotWriteNestedMatrixAsText;
  }

  ofstream output( inFileName.toLocal8Bit() );
  if( saveAsMatrix )
  {
    output << inParam;
  }
  else
  {
    for( int row = 0; row < inParam.NumRows(); ++row )
    {
      int col = 0;
      while( col < inParam.NumColumns() - 1 )
        output << inParam.Value( row, col++ ) << '\t';
      output << inParam.Value( row, col ) << endl;
    }
  }
  return output ? NoError : CouldNotWrite;
}
int
OperatorUtils::LoadMatrix( const QString& inFileName, Param& outParam )
{
  if( inFileName.isEmpty() )
    return MatNotFound;

  if( QFileInfo( inFileName ).suffix() == MATRIX_EXTENSION )
  {
    ParamList paramsFromFile;
    paramsFromFile.Load( inFileName.toLocal8Bit().constData(), true );
    if( paramsFromFile.Size() == 0 )
      return MatNotFound;
    if( paramsFromFile.Size() > 1 )
      return MatMultipleParams;
    Param p = paramsFromFile[0];
    outParam.SetDimensions( p.NumRows(), p.NumColumns() );
    for( int row = 0; row < p.NumRows(); ++row )
      for( int col = 0; col < p.NumColumns(); ++col )
        outParam.Value( row, col ) = p.Value( row, col );
    outParam.RowLabels() = p.RowLabels();
    outParam.ColumnLabels() = p.ColumnLabels();
  }
  else
  {
    ifstream input( inFileName.toLocal8Bit() );
    input.clear();
    vector<vector<string> > matrix;
    string line;
    while( getline( input, line ) )
    {
      istringstream is( line );
      vector<string> row;
      string value;
      while( getline( is, value, '\t' ) )
        row.push_back( value );
      if( !row.empty() )
        matrix.push_back( row );
    }
    if( matrix.empty() )
      return MatNotFound;

    size_t numRows = matrix.size(),
    numCols = matrix[ 0 ].size();
    for( size_t row = 1; row < numRows; ++row )
      if( matrix[ row ].size() != numCols )
        return MatLoadColsDiff;

    outParam.SetDimensions( numRows, numCols );
    for( size_t row = 0; row < numRows; ++row )
      for( size_t col = 0; col < numCols; ++col )
      {
        istringstream iss( matrix[ row ][ col ] );
        iss >> outParam.Value( row, col );
      }
  }
  return NoError;
}
示例#3
0
////////////////////////////////////////////////////////////////////////////////
// MatlabEngine::StringMatrix definitions                                     //
////////////////////////////////////////////////////////////////////////////////
MatlabEngine::StringMatrix::StringMatrix( const Param& p )
    : vector<vector<string> >( p.NumRows(), vector<string>( p.NumColumns() ) )
{
    for( size_t row = 0; row < size(); ++row )
        for( size_t column = 0; column < ( *this )[ row ].size(); ++column )
            ( *this )[ row ][ column ] = p.Value( row, column );
}