Problem building a Matlab equivalent to C-type Char* pointer to a list of strings
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Yuval Porat Avinoam
el 15 de Feb. de 2022
Respondida: Benjamin Thompson
el 15 de Feb. de 2022
Hi,
I need to interface my Matlab code to a C code through the Matlab-C interface.
In the C code there's a function that expects as input a pointer to a list of strings where each string is 30 characters long including terminator.
So in my Matlab code I create the following:
Say the values I want to pass from Matlab to the C code are given in the "labelsArray" variable which is an 8x1 cell array.
say it looks something like this:
labelsArray =
8×1 cell array
{'label1' }
{'label2' }
{'label3' }
{'label4' }
{'label5' }
{'label6' }
{'label7' }
{'label8' }
I do the following:
% Convert labelsArray to string array, pad every entry up to 29 characters,
% and convert the entire thing to a char array
parameter_labels_asChars = char(pad(string(labelsArray),29));
% add the null terminator to each entry (making it 30 characters long including
% termminator) for each entry
parameter_labels_null = obj.nullTerminatedCharArray(parameter_labels_asChars);
% reshape the char array from 8x30 to 1x240
parameter_labels = reshape(parameter_labels_null.',1,[]);
in the end what I send in the calllib function is parameter_labels. and it looks like:
parameter_labels =
'label1 label2 label3 label4 label5 label6 label7 label8 '
where there are 30 cahracters (including spaces and null terminator) between the beginning of each 2 words (including the words themselves).
However, the C application, upon receiving this input, is unhappy, saying it can't find 'label1' parameter.
Now, the execution of the calllib function on the Matlab side is successful. Also, If I run the C function from within the C application with a normal C Char* pointer to a list of strings, the function works fine - so no internal problem with this function.
It seems there's a problem with how I build the Matlab equivalent of the required pointer to a list of strings.
Your help will be very appreciated.
0 comentarios
Respuestas (1)
Benjamin Thompson
el 15 de Feb. de 2022
You may want to review the documentation article "Pass Arguments to Shared C Library Functions". For a NULL terminated string, use char[] as the type in the C function. For a cell array of character vectors, use *char[]. To get more help we may need you to post your C code and C function prototype plus supporting MATLAB code.
0 comentarios
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!