How do I pass a file path/ string in main.c?

24 views (last 30 days)
Tom Engels
Tom Engels on 21 Feb 2022
Commented: Tom Engels on 22 Feb 2022
Hello all,
I have created a shared library with MATLAB Coder. With the programme I want to read data from a path, process it and then output the result again. The input for the MATLAB function is therefore a string (char) of the file path and a few more number arguments (double).
To create the .sh, I have compiled the code accordingly in C and would now like to adapt my main.c. To run it in the Linux terminal, I have to create a new main.c.
In order to be able to execute the .sh in the Linux terminal, I adapt my main function as follows:
int main(int argc, char **argv)
{
char * path =argv[1];
double rpd;
double noc;
rpd = strtol(argv[2],&pEnd_1,10);
noc = strtol(argv[3],&pEnd_2,10);
c_main_test_f(path, rpd, noc);
test_f_terminate();
return 0;
}
In test_f I then pass the values of the function generated by MATLAB Coder. I have defined two return variables for this:
static void c_main_test_f(char path[], double rtg, double az)
{
/* This is autogenerated by MATLAB Coder*/
emxArray_real_T *result_normal;
emxArray_real_T *result_query;
emxInitArray_real_T(&result_query, 2);
emxInitArray_real_T(&result_normal, 2);
printf("Path: %s RTG-value: %f az-value: %f \n", path, rtg, az);
test_f(path, rtg, az, result_query, result_normal);
emxDestroyArray_real_T(result_query);
emxDestroyArray_real_T(result_normal);
}
When I compile this now I get the following error:
/home/user/Desktop/lin_file/test_f.h:26:57: note: expected sconst rtString *e {aka rconst struct > *e} but argument is of type schar *t
In the original function (without input parameters) MATLAB Coder suggests me the following for the string/path:
static emxArray_char_T *argInit_1xUnbounded_char_T(void);
static rtString argInit_rtString(void);
static char argInit_char_T(void);
static char argInit_char_T(void)
{
return '?';
}
static rtString argInit_rtString(void)
{
rtString result;
/* Set the value of each structure field.
Change this value to the value that the application requires. */
result.Value = argInit_1xUnbounded_char_T(pfad_1);
return result;
}
static emxArray_char_T *argInit_1xUnbounded_char_T(void)
{
emxArray_char_T *result;
int idx0;
int idx1;
char *result_data;
/* Set the size of the array.
Change this size to the value that the application requires. */
result = emxCreate_char_T(1, 2);
result_data = result->data;
/* Loop over the array to initialize each element. */
for (idx0 = 0; idx0 < 1; idx0++) {
for (idx1 = 0; idx1 < result->size[1U]; idx1++) {
/* Set the value of the array element.
Change this value to the value that the application requires. */
result_data[idx1] = argInit_char_T();
}
}
return result;
}
And in c_main_test_f:
/* Befor function call*/
rtString path_name;
path_name = argInit_rtString();
/* After function call*/
emxDestroy_rtString(path_name);
Unfortunately, as I am absolutely no C specialist and also new to MATLAB coders, I have no idea how to pass the file path so that my function can use it.
Can anyone help me here?
Many thanks and best regards

Answers (1)

Tom Engels
Tom Engels on 22 Feb 2022
Edited: Tom Engels on 22 Feb 2022
Yes, I can help.
You have to disregard the functions predefined by MATLAB Coder.
Using:
and:
i was able to convert the string/path/group of chars into the struct.
The command in main.c looks like this:
static void c_main_test_f(char *path, double rtg, double az) {
emxArray_char_T *path_str;
/* rows=1, cols = length of the input string */
path_str = emxCreateWrapper_char_T(path, 1, strlen(path));
/* This is autogenerated by MATLAB Coder*/
emxArray_real_T *result_normal;
emxArray_real_T *result_query;
emxInitArray_real_T(&result_query, 2);
emxInitArray_real_T(&result_normal, 2);
printf("Path: %s RTG-value: %f az-value: %f \n", path, rtg, az);
test_f(path_str, rtg, az, result_query, result_normal);
emxDestroyArray_real_T(result_query);
emxDestroyArray_real_T(result_normal);
emxDestroyArray_real_T(path_str);
}
Also you have to include in main.c:
#include <string.h>
#include <stdio.h>
  2 Comments
Tom Engels
Tom Engels on 22 Feb 2022
Edited: Tom Engels on 22 Feb 2022
Now I still have the problem of reading the return values of result_query and result_normal.
My Matlab function returns an array of (n x 3) both times. Where n depends on the size of the read file.
On the one hand, I would like to output this in the terminal and also give this as the return value of the call. (Read the values from emxInitArray_real_T()).
Also, I am not sure if the arrays are initialised correctly. Unfortunately, the documentation is of little help.
-- Docu
void emxInitArray_<name>(emxArray_<name> **pEmxArray, int numDimensions)
Allocates memory for a double pointer to an emxArray.
--
What is meant by "numDimensions"?
Can someone please help me?
Tom Engels
Tom Engels on 22 Feb 2022
numDimensions defines the size of the matrix:
e.g. numDimension = 2 is a struct with (n x n) elements.
One can now simply iterate through this structure, bearing in mind that indexing is done row wise.

Sign in to comment.

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!