'How to export matlab matrix to .txt in a format supported by c
As I state in the title, I want to export a matrix from matlab to a .txt
file in format that is supported by C. What I mean is something like this
{ { 1, 2, 3, 4}, { 5, 6, 7, 8} ...... }
Any suggestions ?
Edit. I used this loop and it seems that it did the trick very nicely. Thanks for the help :)
for (row = 0;row < XLENGTH ; row++)
{
for(column=0;column<YLENGTH;column++)
{
fscanf(fr, "%d " ",", &num);
Image_input[row][column]=num;
}
}
Solution 1:[1]
I think you should be able to use jsonencode() to turn a matrix into the same string format as you want albeit with [] instead of {}. You could then use regexprep() to replace the brackets and fprintf() to write to a file.
That said you could definitely write a function in c using fscanf() and use Matlab's writematrix as others suggested to do something far neater, and probably slightly faster as regexprep will add time.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | liamcsmith |