Nios® II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
Announcements
Intel Support hours are Monday-Fridays, 8am-5pm PST, except Holidays. Thanks to our community members who provide support during our down time or before we get to your questions. We appreciate you!

Need Forum Guidance? Click here
Search our FPGA Knowledge Articles here.
12453 Discussions

Read float values from file stored on SD Card

Altera_Forum
Honored Contributor II
817 Views

Hello all, 

 

I have a matrix with float values which I will be saving in a file from Matlab. I have to copy this file to SD Card and read these values using NIOS 2 while running my program. What is the best method to do this?  

 

Should I simply store the values using fprintf in Matlab? Will I be able to read these values using fscanf in NIOS? 

 

I am using the FAT file functions from the SD Card Audio Demo program but I think those functions read one byte at time. It will be difficult to re-format my data after reading because my values will be stored as float. 

 

Any ideas on how I can implement this? 

 

Thanks
0 Kudos
3 Replies
Altera_Forum
Honored Contributor II
145 Views

Solved the problem myself using fwrite(fid,A,'float32'); in Matlab and using a union of char[4] and float in NIOS, thanks to stackoverflow.com 

 

union { 

char chars[4]; 

float f; 

} u; 

 

Read 4 bytes in u.char and use u.f, it will be exactly what's needed.
Altera_Forum
Honored Contributor II
145 Views

can u send me copies of your NIOS coding.. just for references for my project

Altera_Forum
Honored Contributor II
145 Views

 

--- Quote Start ---  

Solved the problem myself using fwrite(fid,A,'float32'); in Matlab and using a union of char[4] and float in NIOS, thanks to stackoverflow.com 

 

union { 

char chars[4]; 

float f; 

} u; 

 

Read 4 bytes in u.char and use u.f, it will be exactly what's needed. 

--- Quote End ---  

 

 

If the endianess stored in the file by Matlab is the same as NIOS II endianess (it probably it is), you don't need the char[4] array, you can just read and write 4 bytes to and from the address of the float. 

 

BillA
Reply