Prompting user to enter number in MEX code - c

I wonder is there anyway to prompt user to enter integer within MEX code.
Something similar to what input in MATLAB or scanf in C.
I heard about mexCallMATLAB and its use in
str = mxCreateString("Enter extension: ");
mexCallMATLAB(1,&new_number,1,&str,"input");
However I do not really understand what is the point of mxCreateString and what does &str do. I will be really appreciative if anyone can elaborate a little about this or give me another technique to prompt user to enter data.

Let's start from the beginning. mexCallMATLAB calls a MATLAB function, user-defined MATLAB function or MEX file within MEX code. The function declaration is such that:
int mexCallMATLAB(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[],
const char *functionName);
The parameters in detail are:
nlhs: The total number of output parameters that the MATLAB or MEX function is expected to produce.
*plhs[]: An array of pointers where each element is a pointer to an output argument
nrhs: The total number of input parameters that the MATLAB or MEX function is expected to take in.
*prhs[]: An array of pointers where each element is a pointer to an input argument.
functionName: A C string that contains the function name.
Take note that *plhs[] and *prhs[] must be an array of pointers to MEX-type variables. This is important because this will be used to understand what is going to happen next. Using the above logic, take a look at the call to mexCallMATLAB that you have referenced:
mexCallMATLAB(1,&new_number,1,&str,"input");
As we can see, the function to call in MATLAB is the input function which is a MATLAB function where the input argument is the string prompt that is used to display in the Command Window before taking in an input from the user and storing this into the output variable. Take note that what is expected is a numerical expression, usually a number or some operation on numbers.
An example call would look like so:
out = input('Enter a number: ');
Enter a number: would thus be displayed in the Command Window and whatever number you type in gets stored into the variable out.
When using mexCallMATLAB, you are doing the equivalent of the above but invoking this in MEX code. There is one input argument into this function and one output argument that is expected. The second parameter is technically a pointer to an output argument where this would be an array of just one element. The output of input will thus be stored in the variable new_number which is going to contain a number. The str variable is a MEX string that is created using mxCreateString. You must create a MEX string because remember that the expected inputs for the input variables for the function to call through mexCallMATLAB must be MEX variables. Therefore, str is a MEX string and &str would be a pointer to a MEX string. This is also technically an array of pointers with one element as well.
Once this function is called, you put in an input number into the MATLAB Command Window, and thus number is sent back into MEX and stored into new_number in your MEX code.
This seems to be an elegant way to get a variable from the MATLAB Command Window into MEX. I haven't encountered any other method from what I have seen in my MEX experience, so keep using it!

Related

What is the Best Way to Accomplish Homoiconicity in C for this Particular Application?

I am trying to make a complex number grapher in C which takes an expression from the command line and generates a .bmp file.
Here's how I currently have my parser set up:
Node *parser(const char *input); /* `input' is the command line input */
complex eval(Node *queue_head, complex variable_value);
The parser function takes in a string and returns a queue of tokens which each contain an enum representing the type of the value in the Node and a union containing the value. The eval function takes in a queue alongside a value and returns a complex number as the result. This function is called for each pixel when the actual graphing takes place.
This seems terribly inefficient to me, but I'm not sure of a better way of doing this. Some thoughts I've had are turning the inputted expression into a .c file and then compiling it into an object file and including the file all at runtime and just using the function like a normal C function, but the thought of this is abhorrent to me (I also don't know if this is feasible or even possible and I don't have a C compiler on-hand at the moment).
Is there any better way of accomplishing what I want to do in C, despite the language not being homoiconic?

Need help figuring out parameters of __isoc9_scanf()

I have some some C code that I'm trying to understand which uses the function __isoc99_scanf(). I haven't encountered that function ever before. I looked it up and it turns out that it is some kind of variation of scanf(). This is the code:
__isoc99_scanf(&DAT_00400d18,local_78,(undefined4 *)((long)puVar3 + 4));
&DAT_00400d18 is a C string containing the value "%s". local_78 is an array of unknown data type. puVar3 is a pointer that points to the last element of that array.
What really confuses me is why does that function call have three parameters? I know that scanf() takes two parameters: the first one is the format string. The second one is the memory address to save the date into. However __isoc99_scanf() here is invoked with three parameters. I cannot understand why the third parameter is there. The first parameter &DAT_00400d18 is just "%s", which suggests that the second parameter be a memory location where to save that string. But why do you need the third parameter when it's not even specified in the format string?
This is not my code, I didn't write it. Actually it is a disassembled version of the assembly code for a particular application that I'm trying to debug. But I've never seen __isoc99_scanf() before because I only used scanf() in my own code.
When you compile scanf, the compiler automatically translates it to the __isoc99_scanf function in libc. If you compile this code:
#include <stdio.h>
int main() {
char buf[32];
scanf("%32s", buf);
return 0;
}
and decompile in GHIDRA, you get:
__isoc99_scanf(&DAT_001007b4,local_38);
where DAT_001007b4 is "%32s" and local_38 is a buffer. It behaves exactly the same as normal scanf. One important thing to keep in mind when using GHIDRA is it doesn't know exactly how many arguments a function should expect, so if a function is being passed in too many arguments, like in your case, you should just ignore the extra arguments since the code will too.

Use data type according to command line argument in C

I am trying to save the data type given as a command line argument in my C program and use that type in the whole program without checking it. For example, I could run the program "./name -d int" or "./name -d float" and I want the data type to be saved for further use and to be seen in the entire program, not only in the main() function. A short example:
int main() {
/* read command line argument */
/* I would like to be able to save the type in T to use like this */
T a[20];
/* rest of the program */
}
Could I do this?
Thank you.
As pointed out by an earlier answer, you can't do this in c unless you have something like switch statements in your code that handle different cases, because data types are determined at compile time. If you are willing to settle for less than 64 bit precision for integers and you have 64 bit doubles, you can use doubles for all of your numbers and then just have switch statements e.g. when you output, that convert the double to an integer or char etc. as necessary and then output in the desired format.
No you can't do that in C.
Types have to be determined are compile-time, you can't choose the type at runtime.
More dynamic languages (like Objective-C for instance) will allow you to do such things.
Why are you trying to do that in the first place ? Maybe we can provide any more guidance.

How to get the length of an entered argument with GNU get opt

I am trying to 'prettify' some code that makes a log file and in the log file I want to print the name of the input files and what settings were used in a nicely formatted #-box. I need to be able to get the length of an entered argument which has to be stored by the GNU getopt somewhere for it's internal malloc, at least I assume that's how they did it..
Does anyone know how to get that value? I could then use some simple calculus to see howmuch whitespace that I need to add after the last letter of text in my comment box ;)
Where your code stores the value, it is available in the
extern const char *optarg;
variable, and you can simply use strlen() to compute the length of the string.

C - explicit integral as a parameter

I am counting approximation of Pi, I have made it to 3,14596, but I have tried to set integral for counting pi as a parameter when starting program and no result came from it. I tried add when starting program 4/1+x*x (there should by brackets around 1+x*x, but compilator sees them as a mistake), and I get it from args[1] as a 4 only when I transfered char to int with itoa(). Is there any way how to pass it to application for computing? This is the line in my program:
float sum, integral;
integral=4/(1+x*x);
sum=sum+integral;
And I have it in for loop. In this case it counts great, but how to incorporate theese parameter?I have thought of some specific char replacement (I would replace X for an actual value), but is it possible?
You can't pass the formula as argument of your program. Your arg[1] parameter is a string, when you call atoi you make only the conversion of the numbers up to the first nun numeric character, so in your case it converted the '4' character of your string "4/1+x*x" and stopped conversion at the '/'.
If you want to use the formula, you have to write an interpreter that analyzes the string, builds an evaluation tree and execute by replacing the values of the variables. Something quite complicated, probably not under several hundred lines of code.
It's probably easier to use the formula in the source code and recompile the program accordingly. If it's not possible, change to an interpreted language like perl, python, javascript which implement the eval function.

Resources