problem with importing data from matlab to text file

2 次查看(过去 30 天)
Hello
I managed to read the pixel data for a bitmap (24 bit) and got the individual RGB values in the form of arrays for every pixel. I am trying to import this data for use in an Arduino sketch. final task is to use these individual arrays of RGB and display the bitmap on a LED matrix. I started with trying make a text file for each of these individual array. The problem is this:
Used all functions - dlmwrite, xlswrite, fprintf - but in all the cases, the text file being created wraps the whole array data resulting in a jumble. The array size is 267X400. On importing, after few columns, the data starts wrapping. I removed the Word wrap from text file, did everything but simply cannot understand why exactly the data is coming jumbled. The whole data is like a big long paragraph. And the only good thing is that on writing it to an excel file, I get it nicely in the specified rows and columns, but when I try saving excel file data to text file, the same problem occurs and I do not know yet whether I can pass this excel data directly to arduino....so thats why trying for a text file.
Can someone please help me with this. I am completely lost. Any assistance would be greatly appreciated.
Thanks!

采纳的回答

Guillaume
Guillaume 2016-4-6
From your description, it looks like the problem is with the way you view the text file, not with the way it saved. What program are you using to view the text file? Hopefully not notepad which is useless for that.
You can view the file with the matlab editor, or any of the many free text editors available on the net. I use notepad++, but anything slightly more advanced than notepad should be good enough.
  2 个评论
Panda
Panda 2016-4-6
编辑:Guillaume 2016-4-7
Hello @Guillaume
Thanks for replying. I was an idiot....was using notepad! Downloaded notepad++ and i got it perfectly - both ways - matlab-->excel-->txt and also directly matlab-->txt.
Could you please just clarify one thing...I understood how the fprintf function is used and everything..I used the following code:
fprintf(fileID,[repmat('%u\t',1,size(R,2)) '\n'],R');
I tried my best to understand the use of the repmat function but could not. Could you explain the 2nd parameter of the above fprintf function (especially the size(R,2) part). I took help from someone else's question but dont follow it myself...
Thanks!
Guillaume
Guillaume 2016-4-7
The format string is fairly simple, it just replicates '%u\t' (prints number as unsigned integer followed by a tab) as many times as there are columns in R. It follows that with a newline at the end of each row. R has to be transposed as it is passed to fprintf according to its linear indices. Otherwise, it would print the columns of R as rows in the file.
While it works (assuming you want to save your numbers as integer), you could simply replace it (together with the fopen and fclose calls) with:
dlmwrite(filename, R, 'delimiter', '\', precision, '%u');

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by