Thread: Error from Matlab to C (just one line code)

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    33

    Error from Matlab to C (just one line code)

    Hi people. I have en error translating a function from Matlab to C/C++ :


    Code:
    radius(floor(rows/2+1),floor(cols/2+1)) = 1;
     % Get rid of the 0 radius value at the 0
     % frequency point (now at top-left corner)
     % so that taking the log of the radius will 
     % not cause trouble.

    What I did is:


    Code:
    radius[rows/2][cols/2]=1;

    I m sure it s there the problem, because before this line I can print what I want and I can see it at run-time, but after this line I can not print nothing and the work is like blocked. Do you know the error?

  2. #2
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Please show where you define the radius[][] array. Btw, this is the C++ forum. There's a separate forum for C questions.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    33
    Code:
    double **createmat(int rows,int cols){
    Code:
        double **m;
    
        m=new double*[rows];
         for(int i=0;i<rows;i++)
             m[i]=new double[cols];
    
        return m;
    }
    

    In main:

    Code:
    typedef double**matriz;
    
    matriz radius,theta;
    Both are built in a function


  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Given your use of new[], I take it that this is really C++, so call it C++ instead of the ambigious "C/C++" combo.

    Anyway, for starters, please use std::vector or some other appropriate container instead of directly using new[], unless you have some special reasons to do so. Then, post the smallest and simplest (compilable) program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    33
    Hi, the problem is that before using it I used vectors of vectors and I had a problem about glibc. My teacher of University told me I have it because of vector of vectors utilization. So I used this form to do it. Also with that error we did not understand where were the error neither. Can I have an example of what you mean saying a smallest and simplest compilable program. Because to arrive where is that invocation, before there is a lot of code. Maybe I can send you the function where I have the problem.

  6. #6
    Registered User
    Join Date
    Jun 2013
    Posts
    33
    All my project compile. But running it, it stopped in this line his work.This is the function where i have the problem:
    Code:
    void pre_filter_Computations(matriz radius,matriz theta,int cols,int rows){
    
      double X[rows][cols],Y[rows][cols];
      double x[cols],y[rows];
      double epsilon = 0.0001;
    
      for(int i=0;i<cols;i++){
          x[i]=((double)(i-cols)/2)/((double)cols/2);
      }
    
    
      for(int z=0;z<rows;z++){
           y[z]=-(((double)(z-rows)/2)/((double)rows/2));
       }
    
    
     for(int m=0;m<cols;m++){
      for(int n=0;n<rows;n++){
           X[m][n]=x[m];
           Y[m][n]=y[n];
      }
     }
    
          for(int a=0;a<rows;a++){
                for(int b=0;b<cols;b++){
    
                     X[a][b] = pow(X[a][b],2);
                     Y[a][b] = pow(Y[a][b],2);
                     X[a][b] = X[a][b] + Y[a][b];
                      radius[a][b] = sqrt(X[a][b]);
    
              }
          }
        printf("Hello\n"); // it print
        radius[rows/2][cols/2]=1;
       printf("Hello\n"); //doesn' print
          for(int a=0;a<rows;a++){
               for(int b=0;b<cols;b++){
                   radius [a][b]= radius[a][b] + epsilon;
                   theta[a][b] = atan2(Y[a][b],X[a][b])*180/PI;
               }
          }
    }
    

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Smallest and simplest (compilable) program that demonstrates the problem means a program (ie code) that compiles, shows the problem and furthermore, it is as small as you can make it, preferably only a handful lines of code. Anything that doesn't have anything to do with the problem - get rid of it.
    But honestly, if you get an error, you should try to get to the bottom of it instead of trying to recourse to C-style code. You would also do well to switch away from C constructs such as printf and C-style casts. cout is the equivalent of printf, and C++-style casts are the equivalent of C-style casts (google it).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jun 2013
    Posts
    33
    found the error that was in the meshgrid. I mean:
    Code:
    
    
    for(int m=0;m<cols;m++){
      for(int n=0;n<rows;n++){  
         X[m][n]=x[m];      
        Y[m][n]=y[n];  
     }
    }
    Meshgrid :
    Code:
    
    [X,Y] = meshgrid(x,y) transforms the domain specified by vectors x and y into arrays X and Y, which can be used to evaluate functions of two variables and three-dimensional mesh/surface plots. The rows of the output array X are copies of the vector x; columns of the output array Y are copies of the vector y.
    I had to use x vector for cols and y for rows as my teacher said me, I inverted in the innested for rows and cols, so causing a multiply of rows and cols. Now I d like to know if the meshgrid works well. Do you think is right?
    Code:
     for(int m=0;m<rows;m++){ 
       for(int n=0;n<cols;n++){ 
          X[m][n]=x[m];     
          Y[m][n]=y[n];
       } 
    }
    Code:
    
    
    Last edited by Elvio Esposito; 07-02-2013 at 05:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED help, error in the last line of code
    By tobby87 in forum C Programming
    Replies: 8
    Last Post: 12-06-2012, 12:32 PM
  2. Replies: 4
    Last Post: 04-14-2012, 03:52 AM
  3. Running C++ Code from Matlab GUI
    By cpp_novice in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2011, 10:42 AM
  4. glue C and matlab code
    By shaoshao in forum C Programming
    Replies: 1
    Last Post: 06-10-2011, 09:13 AM
  5. MATLAB error
    By chico1st in forum Tech Board
    Replies: 1
    Last Post: 06-17-2008, 03:39 PM