0

Edit:Thank you, The fixed code for those who are interested: ert_main.cpp:

#include <stdio.h>                     /* This ert_main.c example uses printf/fflush */
#include "Rx.h"                        /* Model's header file */
#include "rtwtypes.h"                  /* MathWorks types */
#include <stdlib.h>
#include <iostream>
#include <istream>
#include <sstream> 
#include <fstream>
#include <string> 
//#include <ofstream>//for writing results to file
//#include <ifstream> //doesn't work
#include <vector>
#define lengthOFSignal 5000 

at main func:

 using namespace std;
        std::vector<std::string> words;
         std::string word;
        ifstream  iFile;
        string path = __FILE__; //gets source code path, include file name
        path = path.substr(0,1+path.find_last_of('\\')); //removes file name
        string testFileName = "LEGACY_R12_800BITS_40MHz.dat";
        path+= testFileName; //adds input file to path
        int signal_length=0;

    std::vector<real_T> real_part_VEC, imag_part_VEC;
    std::istringstream ss;
    real_T real_temp, imag_temp;

    iFile.open(path,ios::binary );
     //iFile.open(path.c_str(), ios::binary);
    if (iFile.is_open()) {
         while (std::getline(iFile, word)) {
            words.push_back(word);
        }
        iFile.close();
    }

    signal_length=words.size();
 for(int i=0; i< signal_length;i++)
  {
      ss.str(words[i]);
      ss >> real_temp >> imag_temp;
      real_part_VEC.push_back(real_temp);
      imag_part_VEC.push_back(imag_temp);
  }

    real_T real_part[lengthOFSignal];
    real_T imag_part[lengthOFSignal];

    for (int i=0; i < signal_length; i++) {

        real_part[i]=real_part_VEC[i];
        imag_part[i]=imag_part_VEC[i];
    }

     /* Initialize model */
    Rx_initialize(real_part,imag_part,signal_length);

The OLD code and problem :

enter image description here The .dat file looks like two straight columnes with numbers (spaced)

real and imaginary part

I get an error regarding looping on the readed data (strtok -> atof (Null pointer) )

edited ert_main.cpp main function:

#include <stdio.h>                     /* This ert_main.c example uses printf/fflush */
#include "Rx.h"                        /* Model's header file */
#include "rtwtypes.h"                  /* MathWorks types */
#include <stdlib.h>
#include "mat.h"
#define lengthOFSignal 5000
#define SizeOfLine 35

int_T main(int_T argc, const char *argv[])
{
  /* Unused arguments */
  (void)(argc);
  (void)(argv);


  int i=0;
    char bFileName[] = "QPSK_SISO_802_11_packet_awgn_fr_shft.dat";
    char chr[SizeOfLine*lengthOFSignal];
    char *token;
    real_T real_part[lengthOFSignal];
    real_T image_part[lengthOFSignal];
    int signal_length=0;

    std::ifstream  iFile(bFileName, std::ios::binary);
    iFile.getline(chr,100);
    for(int i=0; i<lengthOFSignal; i++) {
        if (chr== NULL)  break;
                token= strtok(chr," ");
            real_part[i]=atof(token); // real part.---problem occurs here!!!!
            token= strtok(NULL," ");
            image_part[i]=atof(token);// imaginary part.
            iFile.getline(chr,100);
            signal_length++;

    }
    iFile.close();

     /* Initialize model */
    Rx_initialize(real_part,image_part,signal_length);

  /* Attach rt_OneStep to a timer or interrupt service routine with
   * period 40.0 seconds (the model's base sample time) here.  The
   * call syntax for rt_OneStep is
   *
   *  rt_OneStep();
   */
  printf("Warning: The simulation will run forever. "
         "Generated ERT main won't simulate model step behavior. "
         "To change this behavior select the 'MAT-file logging' option.\n");
  fflush((NULL));
  while (rtmGetErrorStatus(Rx_M) == (NULL)) {
    /*  Perform other application tasks here */
      rt_OneStep();
      if(Rx_Y.EVM!=0) break;
  }

  /* Disable rt_OneStep() here */

  /* Terminate model */
  Rx_terminate();
  return 0;
}

link to the solution (VS 2012) link to the project/solution

Idan Banani
  • 131
  • 2
  • 13
  • Have you debugged through the code and checked the parsing loop? – Timo Geusch Sep 26 '13 at 18:05
  • @shunyo The C code is automatically generated from a MATLAB/Simulink model, check the header. Manual editing of the code is not recommended. Fixing the model and regenerating he code is the correct way to approach this. – am304 Sep 26 '13 at 20:24
  • @am304 my fault. Deleting the comment. – shunyo Sep 26 '13 at 20:26
  • am304 : The input for the main subsystem of the model is a signal real part and imaginary part + (and in future project the info bits too). connected as inports (Outputs can be "To workspace), all of the input ports doe's not appear in the genarated code on purpose- they are not constant, thus I need to import the values by extracting it from a .dat/.mat file. Therefore I need to edit the main function/ Initiallize function. other constant from parameters such as from workspace inputs and parameters in the subsystem were choosen to be inline for storage class. – Idan Banani Sep 27 '13 at 07:42
  • @Timo Geusch- Yes, I tried debugging, the value inserted to the variable chr was marked in red , It was some sort of garbage / Null string / zero – Idan Banani Sep 27 '13 at 09:30

1 Answers1

1

You are trying to convert more than the input length. That is creating the debug assertion error. As a suggestion, try to read the data in as numbers. Makes life a lot easier.


Edit: I am adding onto the previous statement. I have used istringstream to parse the string into a number. I just checked this for a number but you can easily expand this.

std::string str("2.1506668e-03");
std::istringstream ss;
ss.str(str);
double x;
ss >> x;
std::cout << x << std::endl;

Ans: 0.0021567


New edit: Ah! This code is much better. But you still need to iron out some very basic errors.

1> The file.is_open failing can only be due to the file not being found. Try to find out if the file is in the search path. The easiest way to do it is copying the file onto the project file folder.

2> Using vectors always make sense when the size is undetermined. Btw, you can determine the size of a file and thus the size of the arrays using fseek and ftell.

3> Just a cursory glance reveals that this statement std::string real_str("words[i]");should be changed to std::string real_str(words[i]); The previous one takes the string words[i] as input.

4> In the for loop, you are looping for the signal_length but you are using words[i] and words[i+1]. So only half the vector would be read in such a case.

I would just read the line as a whole into the word vector and then parse it into real and imaginary parts

 std::vector<std::string> words;
 std::string word;
 if (fin.is_open()) {
    while (std::getline(fin, word)) {
        words.push_back(word);
    }
    fin.close();
 }


// I would declare two vectors 
std::vector<real_T> real_part, imag_part;
std::istringstream ss;
real_T real_temp, imag_temp;

// for loop
for(int i=0;i<words.size();i++)
{
      ss.str(words[i]);
      ss >> real_temp >> imag_temp;
      real_part.push_back(real_temp);
      imag_part.push_back(imag_temp);
}
shunyo
  • 1,277
  • 15
  • 32
  • Can I get into a state I have in each for loop iteration the relevant string stored in a variable with using the above library functions? – Idan Banani Sep 28 '13 at 09:28
  • I tried using vectors in order to do the above (I got an error regarding allocating space for the real and imag part arrays - the size of the array is known only at runtime, so I switched to vectors. but the whole converted code use arrays of real_T. =/ . can I use the vector size somehow to create an array from the vector like in http://stackoverflow.com/questions/7488039/expression-must-have-a-constant-value-error-in-c , or should I allocate enough memory beforehand? – Idan Banani Sep 28 '13 at 10:26
  • As I mentioned, there is no point using two vectors instead of two arrays for this project... ( I need to pass to the initiallize functions arrays of real_T) – Idan Banani Sep 28 '13 at 13:28
  • I mentioned how to get the size of the arrays from the size of the file. Then dynamically allocate the arrays using the size. – shunyo Sep 28 '13 at 13:30
  • quick question: word was like: "number number/r", how did ss.str(words[i]); ss >> real_temp >> imag_temp; knew it should cut the /r? . second: About caluclating the dynamic size, I guess counting the number of lines won't help (same compile error), so you are suggesting to use something like seek pointer? (I'll read about it later). – Idan Banani Sep 28 '13 at 13:55
  • This feature is known as parsing and is used very often for converting formatted strings. If you would like to know more about splitting strings I suggest you read stringstream and also this question (http://stackoverflow.com/questions/236129/splitting-a-string-in-c). – shunyo Sep 28 '13 at 14:09
  • For finding the size of the file, you can seek to the end of file and do a tell to find the size. Then you can divide it up on the basis of the size of the array. – shunyo Sep 28 '13 at 14:10