Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Among my little understanding in programming I'm writing this code in C to use it in a S-Function block on Matlab/Simulink. I've been trying to follow the same logic as other S-Function codes in C [timestwo.c] and wrote the following code. However, although I think 'it works' in my simulation I can't understand well how does every execution line inside if-statements works. For example, at the first "if" condition I want to compare two currents going into this S-Function block. In case Snubber Current - *u0_Is[i] is less than Snubber Reference - *u1_Iref[i], then a gate-signal (a 1) must be delivered to an electronic device (IGBT). Now, what's the idea of writing *IGBT++ = 1 ?. I want to know if it is correct to see *IGBT as a vector or array that points to a variable stored in memory, so if I write *IGBT = 1, it will save this `1` at that location?.

Thanks in advance.

static void mdlOutputs(SimStruct *S, int_T tid)
{
    int_T             i;
    // Snubber Current
    InputRealPtrsType u0_Is = ssGetInputPortRealSignalPtrs(S, 0);
    // Triangle Reference
    InputRealPtrsType u1_Iref = ssGetInputPortRealSignalPtrs(S, 1);
    //  Voltage THY - colector/emisor
    InputRealPtrsType u2_Vce = ssGetInputPortRealSignalPtrs(S, 2);


    real_T            *IGBT = ssGetOutputPortRealSignal(S, 0);
    real_T            *THY =  ssGetOutputPortRealSignal(S, 1);
    int_T             width = ssGetOutputPortWidth(S, 0);
    int_T             width1 = ssGetOutputPortWidth(S, 1);


for (i=0; i<width; i++){

        if (*u0_Is[i] < *u1_Iref[i]){
            *IGBT++ = 1;
            if (*u2_Vce[i] >= 0.8 && (*u0_Is[i] < *u1_Iref[i]) ){
                *THY++ = 1;
            }
            else {
                *THY++ = 0;
            }
        }
        else {
            *IGBT++ = 0;
            *THY++ = 0;

        }
    }
}


What I have tried:

Deleting the ++ signs in the output pointers, getting the same result when plotting.
Posted
Updated 23-Mar-18 3:08am

1 solution

You are correct, *IGBT is a pointer to an array of size width. You access array elements by using pointer dereference (*) or an index ([]):
C++
// These are similar and will set an array item to the assigned value
*IGBT = val;
IGBT[0] = val;

The ++ operator will increment the value of the variable. In your case it is a postfix operator (appended to the variable name) which will increment after other operations:
C++
*IGBT++ = val;
// is the same as
*IGBT = val;
IGBT = IGBT + 1;
Omitting the increment in your case would only set the first element of the array which is probably not what you want.

But you can simplify your code by performing the increment outside the conditions:
for (i=0; i<width; i++){
    if (*u0_Is[i] < *u1_Iref[i]){
        *IGBT = 1;
        if (*u2_Vce[i] >= 0.8 && (*u0_Is[i] < *u1_Iref[i]) ){
            *THY = 1;
        }
        else {
            *THY = 0;
        }
    }
    else {
        *IGBT = 0;
        *THY = 0;
        }
    }
    *IGBT++;
    *THY++;
}
or just use the index i instead:
for (i=0; i<width; i++){
    if (*u0_Is[i] < *u1_Iref[i]){
        IGBT[i] = 1;
        if (*u2_Vce[i] >= 0.8 && (*u0_Is[i] < *u1_Iref[i]) ){
            THY[i] = 1;
        }
        else {
            THY[i] = 0;
        }
    }
    else {
        IGBT[i] = 0;
        THY[i] = 0;
        }
    }
}
 
Share this answer
 
Comments
CPallini 23-Mar-18 9:27am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900