I have a matrix A
where I want to set out f
for every number. The problem is some numbers are integers, and then it will collide.
Let's say we have the matrix X_point
and I want to convert it into a C float array, without brackets.
px = strrep(mat2str(X_point), ']', 'f');
px = strrep(px, '[', '');
px = strrep(px, ' ', 'f,');
px = strrep(px, ';', 'f,');
px = strrep(px, ',0f', ',0.0f');
px = strrep(px, ',0,', ',0.0f,');
The issue here is that if I got an integer, non 0
number, then the result will look like this. You see the problem here?
px = 45.8719f,57.6531f,59.3361f,57.2791f,47.9289f,37.6438f,37.8308f,45.8719f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,
0.0f,0.0f,0.0f,0.0f,0.0f,67.5643f,79.5325f,83.8336f,83.6466f,78.5975f,69.8083f,61.5802f,55.0351f,53.913f,55.7831f,56.9051f
,67.5643f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,12.7723f,18.5694f,20.4395f,22.1225f,21.1875f,19.6914f,16.3254f
,11.4633f,8.8453f,8.8453f,2.3001f,0.6171f,-0.3179f,-3.1024f,-4f,-4.2244f,-4.2244f,-4.2244f,1.8345f,4.7518f,7.2202f,12.7723
f,72.9705f,74.5414f,80.8247f,94.7377f,96.533f,100.5722f,101.8177f,99.7232f,94.7489f,90.2983f,84.015f,78.2553f,75.6372f,72.
9705f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,8.6152f,34.7957f,32.9631f,0.2375f,8.6152f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f
,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f
This is the problem ,-4f,
My question is simple. How can I say that any number in ,Xf
should be a floating point number?
If there is a way to convert every integer number in matrix X_point
, then this would be much easier.
CodePudding user response:
Solved the issue.
To convert a MATLAB matrix X_point
into C-array px
with float expresion
px = sprintf('%0.4ff,', X_point);
px(end) = ''; % Remove the last ','
CodePudding user response:
What is px
, X_point
, mat2str
and strrep
? How do you generate the output, and how does it differ from what you expect? And the value -4
is perfectly valid as floating point.
CodePudding user response:
The context for your question is unclear. Are you writing a C function to convert a string to change the syntax? Your code is not proper C code for this, you must use string literals, not character constants. What is the source format? What is the destination format?
I would strongly advise to consider parsing the string one number at a time and produce the expected syntax. String substitution is throw away code leaving some corner cases unhandled, such as integers as posted but also the case of an initial 0 value.
Here is a simplistic approach, assuming sufficient array sizes:
void convert(char *dest, const char *src) {
int seen_dot = 0, seen_digit = 0;
size_t i = 0, k = 0;
for (;;) {
unsigned char c = src[i ];
if (isdigit(c)) {
dest[k ] = c;
seen_digit = 1;
} else
if (c == '.') {
// one might want to convert .1 to 0.1
dest[k ] = c;
seen_dot = 1;
} else {
if (seen_digit) {
if (!seen_dot) {
dest[k ] = '.';
dest[k ] = '0';
}
dest[k ] = 'f';
}
seen_digit = seen_dot = 0;
if (c == '\0') {
dest[k] = c;
break;
}
if (c == ' ') {
dest[k ] = ',';
} else
if (c == '-' || c == ' ') {
dest[k ] = c;
}
// ignore other characters
}
}
}