1 / 12

Computer Simulation Lab

Computer Simulation Lab. “Lecture 11”. Electrical and Computer Engineering Department SUNY – New Paltz. Importing and exporting data. Objectives • the load and save commands; • low-level file I/O functions. Save Command. SAVE Save workspace variables to disk.

kane-burton
Download Presentation

Computer Simulation Lab

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Simulation Lab “Lecture 11” Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz

  2. Importing and exporting data Objectives • the load and save commands; • low-level file I/O functions. SUNY-New Paltz

  3. Save Command SAVE Save workspace variables to disk SAVE FILENAME saves all variables to binary “filename.mat" SAVE FILENAME X Y Z saves X, Y, and Z SAVE ... -ASCII SUNY-New Paltz

  4. Examples A =[1 2 3 ; 4 5 6] save myData A View myData.mat with a text editor save myData -ascii SUNY-New Paltz

  5. Load Command load myData load myData.txt A=load(‘myData.txt’) SUNY-New Paltz

  6. Low-level file I/O functions SUNY-New Paltz

  7. Low-level file I/O functions fid = fopen(’marks.bin’, ’w’) fwrite(fid, name) File Handle fread(fid, 78, ’float’); fclose(fid) SUNY-New Paltz

  8. Low-level file I/O functions Pointer Update fid = fopen(’marks.bin’, ’w’) fwrite(fid, name) Open for Write Variable Name SUNY-New Paltz

  9. Low-level file I/O functions Pointer Update fid = fopen(’marks.bin’, ’r’) A=fread(fid, 16, ‘float’) Variable Name Open for Read Variable Size&Type SUNY-New Paltz

  10. Examples 1- Create a binary file named “odd.bin” that contains all the odd numbers between 1 and 99. Examine the file by text editor. 2- Open the binary file and read its contents into an array named OddNums. 3- Append the even numbers between 2 and 100 to the above file. Examine the file by text editor. SUNY-New Paltz

  11. MATLAB Code fid=fopen('test1.bin','a') x=[2:2:100]; fwrite(fid,x) fclose(fid); fid=fopen('test1.bin','r') y = fread(fid) fclose(fid); fid=fopen('test1.bin','w') x=[1:2:100]; fwrite(fid,x) fclose(fid); fid=fopen('test1.bin','r') y = fread(fid) fclose(fid); SUNY-New Paltz

  12. Example Create a text file named ‘ASCII.TXT’ that contains all the ASCII characters between 0 and 255. Use a text editor to examine the file. fid=fopen('ASCII.TXT','w') x=[1:255]; fwrite(fid,char(x)) fclose(fid); SUNY-New Paltz

More Related