on variadic function in C when porting matlab files to C

D

dd

Hi, all,

I found many limitation of C in the process of porting some matlab
files to C, but I'm going to stick to it this time. One problem is the
variadic function. C doesn't have overload
mechanism, right? And to use va_list and va_arg, etc. to realize the
variable
number of inputs to a function is OK, but I don't know how to express
the
missing variables in the function like below . Thank you very much if
anyone could help me out!

The matlab part is like below

function [output_firing_time, x,
y]=calculate_neuron(input_firing_times,delays,weights,tau,threshold,sim_time)

disp_fire_info=0;

%Activation Threshold
if nargin < 5 || threshold==-1
threshold=2;
end
%Time constant for spike response function
if nargin < 4 || tau==-1
tau=2;
end
%Delays
if nargin < 3 || min(delays==-1)
delays=[1 1 1];
end
%Weights
if nargin < 2 || ~iscell(weights)
weights=[1 1 1];
end
%Input Firing Times
if nargin < 1 || min(input_firing_times==-1)
input_firing_times=[0 1 2];
end

output_firing_time=sim_time(3)+1;

The c part

/*********************************************************/
*calculate_neuron(float *num_var,...)

{
va_list ap;
int count = 0;
float temp=numinputvar;
// to calculate the number of inputs
float x;
float ***y;
float output_firing_time;
float t_begin, t_interval,t_end;
float t_index[1000]=0;
float current_delay;
// int num_var=0;
int max=0;
int start_index=0;
// int num_inputs=0;
int j=1,i,k,n;
int indexdelay,index_input_firing;
int disp_fire_info=0;

va_start(ap,num_var);
//need to change, for input_firing times might be bigger than 100
// Basic Spiking Neural Network
while( temp != -10000.00000000000000 )
{
count++;
temp = va_arg( ap, float);
}
va_end( ap );

//Activation Threshold
if (num_var<5 || *threshold==-1)
*threshold=2;

//Time constant for spike response function
if (num_var<4 || *tau==-1)
*tau=2;
//delays
if (*delays==-1||*(delays+1)==-1||*(delays+2)==-1)
indexdelay=1;
if (num_var<3 ||indexdelay==1) // ||delays)
min(delays==-1) how to
port?
*delays=[1 1 1];
//weights

if (num_var<2 ) //~iscell(weights) ??
*weights=[1 1 1];
//Input Firing Times

if(*input_firing_times==-1||*(input_firing_times+1)==-1||*(input_firing_time
s+2)==-1)
index_input_firing=1;
if (num_var<1 || index_input_firing==1)
input_firing_times=[0 1 2];
 
J

Joakim Hove

Hello,

I found many limitation of C in the process of porting some matlab
files to C, but I'm going to stick to it this time.

I don't have any spesific help/comments to your problem, but my
general answer is that attempts to translate (more or less) directly
matlab -> C is probably bound to give your large amounts of grief.

If you really have to do this I would strongly recommend thinking
carefully through the problem and the solution, and expressing it in a
way closer to the 'C' spirit (whatever that might be) than directly
translating the matlab code.

Good luck.

Joakim

--
Joakim Hove
hove AT ntnu.no /
Tlf: +47 (55 5)8 27 13 / Stabburveien 18
Fax: +47 (55 5)8 94 40 / N-5231 Paradis
http://www.ift.uib.no/~hove/ / 55 91 28 18 / 92 68 57 04
 
T

tmp123

dd said:
The matlab part is like below

function [output_firing_time, x,
y]=calculate_neuron(input_firing_times,delays,weights,tau,threshold,sim_time)

disp_fire_info=0;

%Activation Threshold
if nargin < 5 || threshold==-1
threshold=2;
end


Why not something like...?

void calculate_neuron(float num_var,...)
{
...
tau=-14;
threshold=10;
... some more defaults

va_start(ap,num_var);
switch(num_var)
{
case 2:
tau=va_arg(ap,float);
case 1:
threshold=va_arg(ap,float);
}
va_end(ap);

...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top