Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to use a C code as a function in Matlab ?

Status
Not open for further replies.

ramone

Member level 3
Joined
Oct 5, 2003
Messages
58
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Location
Patras University
Activity points
721
MATLAB help! urgent!

hi there!

i have write down a code in c... it exports a linked list of structures.... i want to use it as a function in matlab and i want to export that list as a matrix.... does anybody know how????
 

Re: MATLAB help! urgent!

Umm i think you should perhaps put your code on here so we have more of an idea what you're on about...

regards,
gvanto
 

Re: MATLAB help! urgent!

are you looking at something like this,

**broken link removed**
**broken link removed**
 

Re: MATLAB help! urgent!

if u really want help u must explain it more depth about your problem and put some of ur core code here....
 

Re: MATLAB help! urgent!

/******************************************************************************/
/* MAIN_C
/******************************************************************************/
#include <stdio.h>
#include "gridTree.h"

/* THE GRID
|-----------------------------------------------
| | | | |
|(H-1)W +1 |(H-1)W +2 | .... | H*W |
| | | | |
|---------------------------------------------- |
I | |
| ........................... |
| | |
| |---------------------------------------------- |
| | |
| | ........................... |
| | |
| |---------------------------------------------- |
| | | | | |
GRID_H (H) | W+1 | W+2 | ...... | 2W |
| | | | | |
| |---------------------------------------------- |
| | | | | |
| | 1 | 2 | .. | W |
| | | | | |
| -----------------------------------------------
---------- GRID_W (W) --------> J
*/

/* THE PERMITTED MOVES ARRAY
|-----------------------------------------------
| | | | |
|(H*W-1)* | | .... |(H*W)(H*W) |
| (H*W) +1 | | | |
|---------------------------------------------- |
I | |
| ........................... |
| | |
| |---------------------------------------------- |
| | |
| | ........................... |
| | |
| |---------------------------------------------- |
| | | | | |
H*W | (H*W)+1 | (H*W)+1 | ...... | 2(H*W) |
| | | | | |
| |---------------------------------------------- |
| | | | | |
| | 1 | 2 | .. | H*W |
| | | | | |
| -----------------------------------------------
---------- H * W --------> J
*/


int GRID_W = 8;
int GRID_H = 8;
int obstacles[] = {26,34,35,42,43,44};
#define DEBUG 1

/******************************************************************************/
/* printPermittedMovesArray
/******************************************************************************/
void printPermittedMovesArray(int* x)
{
int nGridCells = GRID_W * GRID_H;
int i=0;
int j=0;
int i_j_diff;

for(i=1; i<= nGridCells; i++)
{
for(j=1; j<= nGridCells; j++)
{
fprintf(stderr, " X[%02d][%02d]=%03d ", i, j, x[((i-1)*nGridCells) + (j-1)]);
}
fprintf(stderr, "\n\n");
}
}
/******************************************************************************/
/* isObstacle
/******************************************************************************/
int isObstacle(int cell)
{
int i;
int obstaclesArrayWidth = sizeof(obstacles) / sizeof(int);

for(i=0; i< sizeof(obstacles); i++)
{
if(cell == obstacles)
return 1;
}

return 0;
}

/******************************************************************************/
/* CreatePermittedStepsArray
/******************************************************************************/
int * CreatePermittedStepsArray()
{
// number of grid cells
int nGridCells = GRID_W * GRID_H;
// Permitted moves array
int * x;
// Permitted moves array variables
int i, j, i_j_diff;

// allocate memory
x = (int *)calloc(nGridCells * nGridCells, sizeof(int));

// check if the allocation is ok
if(!x)
{
#ifdef DEBUG
fprintf(stderr, "Error allocating memory for permitted moves array\n");
#endif
return ((int*)0);
}

// The array has an 1 at i,j cell if the move from i cell to j cell is permitted
for(i=1; i<= nGridCells; i++)
{
for(j=1; j<= nGridCells; j++)
{
i_j_diff = j - i;
if( (i_j_diff == 1) /*RIGHT*/ || (i_j_diff == -1)/*LEFT*/ || (i_j_diff == GRID_W) /*UP*/ || (i_j_diff == -GRID_W) /*DOWN*/)
{
// x[j] -- i and j -1 because the array starts counting at 0
x[((i-1)*nGridCells) + (j-1)] = 1;
}
if(isObstacle(j))
{
x[((i-1)*nGridCells) + (j-1)] = 0;
}
}
}

return x;
}
/******************************************************************************/
/* BuildChildren
/******************************************************************************/
int getPermittedSteps(int fromCell, int* toCells, int*PermittedStepsArray)
{
int j=0;
int i= fromCell;
int nCells = 0;
// number of grid cells
int nGridCells = GRID_W * GRID_H;


// check all the i=fromCell line of PermittedStepsArray for aces
for(j=1; j<=nGridCells; j++)
{
if( PermittedStepsArray[((i-1)*nGridCells) + (j-1)] == 1)
{
toCells[nCells] = j;
nCells++;
}
}
return nCells;

}
/******************************************************************************/
/* printPath
/******************************************************************************/
void printPath(struct treeNode* node)
{
struct treeNode * tmpNode = node;
while(tmpNode->parent != 0)
{
fprintf(stderr, "%03d <-", tmpNode->cell);
tmpNode = tmpNode->parent;
}
fprintf(stderr, "%03d\n", tmpNode->cell);
return;
}
/******************************************************************************/
/* BuildChildren
/******************************************************************************/
void BuildChildren(struct treeNode* parentNode, int* PermittedStepsArray, struct tree * tree)
{
int i;
int nCells;
int toCells[4];
struct treeNode * newChild;

// Check if we have already the max_steps
if(parentNode->depth >= tree->max_steps)
{
//printPath(parentNode);
return;
}

nCells = getPermittedSteps(parentNode->cell, toCells, PermittedStepsArray);
for(i=0; i< nCells; i++)
{
// found a new child
tree->n_nodes++;
newChild = &tree->treeNodeList[tree->n_nodes -1];
newChild->depth = parentNode->depth +1;
newChild->parent = parentNode;
newChild->cell = toCells;
BuildChildren(newChild, PermittedStepsArray, tree);
}
}

/******************************************************************************/
/* CreateTree
/******************************************************************************/
void CreateTree(int entryPointCell, int max_steps, int* PermittedStepsArray, struct tree * tree)
{
// max number of nodes is 4^max_steps
int max_nodes=1,i=0;
int powFour=1;
for(i=0; i<=max_steps; i++)
{
max_nodes+= powFour;
powFour = powFour *4;
fprintf(stderr,"MAX NODES= %d\n" ,max_nodes);
}
fprintf(stderr, "Creating tree with:\n\tEntryPoint=%d\n\tMax Steps=%d\n\tMaxNodes=%d\n\n",entryPointCell,max_steps,max_nodes);
// Allocate memory for all nodes ---> tree->treeNodeList[max_nodes]
tree->treeNodeList = (struct treeNode *)calloc(max_nodes, sizeof(struct treeNode));
if(!tree->treeNodeList)
{
fprintf(stderr, "Could not allocate memory for nodes\n");
}

tree->max_steps = max_steps;
// Initialize entry point
tree->n_nodes = 1;
tree->entry_point = &tree->treeNodeList[0];
tree->entry_point->depth = 0;
tree->entry_point->parent = 0;
tree->entry_point->cell = entryPointCell;

BuildChildren(tree->entry_point, PermittedStepsArray, tree);

return;
}

/******************************************************************************/
/* printTree
/******************************************************************************/
void printTree(int steps, struct tree * tree)
{
// Find all the nodes with depth = steps
// can not be more than 4^steps
int i;
int n_destin = 0;
struct treeNode ** destinations;
int max_destin=1;

for(i=0;i<=steps; i++)
{
max_destin = max_destin * 4;
}
destinations = (struct treeNode**)calloc(max_destin, sizeof(struct treeNode*));
if(!destinations)
fprintf(stderr, "Could not allocate memory for destinations\n");

for(i=0; i< tree->n_nodes; i++)
{
if(tree->treeNodeList.depth == steps)
{
n_destin++;
destinations[n_destin-1] = &(tree->treeNodeList);
}
}

for(i=0; i< n_destin; i++)
{
fprintf(stderr, "%09d.Destination:%03d ---- ", i+1, destinations->cell);
printPath(destinations);
}

free(destinations);
return;

}

/******************************************************************************/
/* main
/******************************************************************************/
int main ()
{
int choice;
// Permitted moves array
int * x;
// tree with linked nodes
struct tree tree;

x = CreatePermittedStepsArray();
#ifdef DEBUG
//printPermittedMovesArray(x);
#endif

CreateTree(1, 9, x, &tree);
#ifdef DEBUG
printTree( 6, &tree);
#endif

free(tree.treeNodeList);
free(x);
printf("na teleiwsei?\n");
scanf("%d%",&choice);
return 1;


HEX FILE:

/******************************************************************************/
/* GRIDTREE_H
/******************************************************************************/

#ifndef GRIDTREE_H
#define GRIDTREE_H

struct treeNode{
int depth;
int cell;
struct treeNode *parent;
};

struct tree{
struct treeNode* entry_point;
struct treeNode* treeNodeList;
int n_nodes;
int max_steps;
};
#endif /*GRIDTREE_H*/


I want to be able to call from matlab PermittedStepsArray, CreateTree, PrintTree and so on...

PLZ help!
 

Re: MATLAB help! urgent!

I don't really know if what you want can be achieved in MATLAB, but my guess is that few actually do it this way. In the first place, most of your functions have no return types. MATLAB is not so smart as to intuitively know that the first or second argument contains the returned data.

They either code completely in C/C++ and call MATLAB dlls, or they write their outputs to data files and read the data files from MATLAB. I think MATLAB data files are with .mat extensions. You can google for the format.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top