Very large table for searching C ++ - can I avoid typing all of this?

I am not a programmer, but I am an engineer who needs to use C ++ encoding for this, so sorry if this question is a bit basic.

I need to use a lookup table as I have some high linear dynamics that I need to simulate. It consists of literally 1000 pairs of values, from the pair (0.022815, 0.7) to (6.9453, 21.85).

I don't want to put all these values ​​into my C code. The values ​​are currently stored in Matlab. Can I read them from a .dat file or something similar?

I calculated the value and I just want the program to throw out the paired value.

Thank,

Adam

+3


source to share


7 replies


You cannot read something stored in Matlab directly unless you want to write a parser for whatever format Matlab stores its data in. I'm not familiar with Matlab, but I would be very surprised if it didn't have a function to output this data to a file, in some text format, that you can read and parse.

Assuming it's constant data, if it can output something along the line:

{ 0.022815, 0.7 },
       ...
{ 6.9453, 21.85 },

      



you can include it as a table initializer in C ++. (It might look odd to have #include

a variable in the middle of a variable definition, but it's perfectly legal and perfectly justified in such cases.) Or just copy / paste it into your C ++ program.

If you can't get exactly this format directly, it should be trivial to write a small script that converts whatever format you get to this format.

+6


source


this program defines the card, then reads the a.txt file, inserts it onto the card, iterates over the card for whatever your purpose, and finally writes the card to the file. just simple practice:



#include <fstream>
#include <iostream>
#include <map>
using namespace std;

int main(){
    ifstream inFile("a.txt", ios::in);

    if (! inFile ){ 
        cout<<"unabl to open";
        return 0;
    }

    //reading a file and inserting in a map
    map<double,double> mymap;
    double a,b;
    while( ! inFile.eof()  ){
         inFile>>a>>b;
         mymap.insert ( a,b );
    }
    inFile.close(); //be sure to close the file

     //iterating on map
     map<double,double>::iterator it;
     for ( it=mymap.begin() ; it != mymap.end(); it++ ){
          // (*it).first  
          // (*it).second 
     }

     //writing the map into a file
     ofstream outFile;
     outFile.open ("a.txt", ios::out); // or ios::app if you want to append 

     for ( it=mymap.begin() ; it != mymap.end(); it++ ){
        outFile << (*it).first << " - " << (*it).second << endl; //what ever!
     }

     outFile.close();
     return 0;
}

      

+1


source


What I would do for this is what I think is faster than opening the file and closing it. First of all, create a header file that contains all the data in the array. You could "replace everything" in Notepad or so to replace curly braces () with curly braces {}. Later, you can even write a script that makes the header file from the Matlab file

>> cat import_data.h
#define TBL_SIZE 4 // In your case it is 1000

const double table[TBL_SIZE][2] =
{
    { 0.022815, 0.7 },
    { 6.9453, 21.85 },
    { 4.666, 565.9},
    { 567.9, 34.6}

};

      

Now in the main program you include this header for the data too

>> cat lookup.c
#include <stdio.h>
#include "import_data.h"

double lookup(double key)
{
    int i=0;
    for(;i<TBL_SIZE; i++) {
        if(table[i][0] == key)
            return table[i][1];
    }
    return -1; //error
}


int main() {
    printf("1. Value is %f\n", lookup(6.9453));
    printf("2. Value is %f\n", lookup(4.666));
    printf("3. Value is %f\n", lookup(4.6));
    return 0;
}

      

+1


source


Yes, you can read them from dat file. The question is what format is dat file? Once you know this, you want to use:

fopen
fread
fclose

      

for C and

ifstream

      

for C ++ (or something similar).

0


source


The program should still get these pairs from the file and load them into memory. You can loop through the lines in the file, parse pairs, and insert them into std :: map. Something like that:

#include<fstream>
#include<map>
...
ifstream infile("yourdatfile.dat");
std::string str;
std::map<double, double> m; //use appropriate type(s)
while(getline(infile, str)){
 //split str by comma or some delimiter and get the key, value
 //put key, value in m
}
//use m

      

0


source


For signal processing instrumentation, you can export data to C header files directly from Matlab (don't know if this is your specific case):

Matlab export to C header

Or maybe the following article can help:

Export / Import Data to / from MATLAB

0


source


One option is to create a C ++ lookup table in matlab. Just write to a text file (lookup.cpp), read the table creating the C ++ source ...

-1


source







All Articles