Importing BinaryData withLow-LevelI/O
ImportingBinaryDatawithLow-LevelI/O
Inthis section...
“Low-LevelFunctionsforImportingData”onpage2-117
“ReadingBinaryDatainaFile”onpage2-118
“ReadingPortionsofaFile”onpage2-120
“ReadingFilesCreatedonOtherSystems”onpage2-123
“OpeningFileswithDifferentCharacterEncodings”onpage2-124
Low-LevelFunctionsforImportingData
Low-levelfileI/Ofunctionsallowthemostdirectcontroloverreadingor
writingdatatoafile. However,thesefunctionsrequirethatyouspecify
moredetailedinformationaboutyourfilethantheeasier-to-usehigh-level
functions.Foracompletelistofhigh-levelfunctionsandthefileformatsthey
support,see“SupportedFileFormats”onpage1-2.
Ifthehigh-levelfunctionscannotimportyourdata,useoneofthefollowing:
fscanf
,whichreadsformatteddatainatextorASCIIfile;thatis,afile
youcanviewinatexteditor.Formoreinformation,see“ReadingDataina
FormattedPattern”onpage2-29.
fgetl
and
fgets
,whichreadonelineofafileatatime,whereanewline
characterseparateseachline.Formoreinformation,see“ReadingData
Line-by-Line”onpage2-32.
fread
,whichreadsastreamofdataatthebyteorbitlevel. Formore
information,see“ReadingBinaryDatainaFile”onpage2-118.
Note Thelow-levelfileI/OfunctionsarebasedonfunctionsintheANSI
StandardCLibrary.However,MATLABincludesvectorizedversionsofthe
functions,toreadandwritedatainanarraywithminimalcontrolloops.
2-117
Pdf compression - Compress reduce PDF size in C#.net, ASP.NET, MVC, Ajax, WinForms, WPF
C# Code & .NET API to Compress & Decompress PDF Document
adjust pdf page size; change font size in fillable pdf
Pdf compression - VB.NET PDF File Compress Library: Compress reduce PDF size in vb.net, ASP.NET, MVC, Ajax, WinForms, WPF
VB.NET PDF Document Compression and Decompression Control SDK
pdf optimized format; best way to compress pdf files
2
Importing Data
ReadingBinaryDatainaFile
Aswithanyofthelow-levelI/Ofunctions,beforeimporting,openthefilewith
fopen
,andobtainafileidentifier.Whenyoufinishprocessingafile,closeit
with
fclose(fileID)
.
Bydefault,
fread
readsafile1byteatatime,andinterpretseachbyteas
an8-bitunsignedinteger(
uint8
).
fread
createsacolumnvector,withone
elementforeachbyteinthefile.Thevaluesinthecolumnvectorareofclass
double
.
Forexample,considerthefile
nine.bin
,createdasfollows:
fid = fopen('nine.bin','w');
fwrite(fid, [1:9]);
fclose(fid);
Toreadalldatainthefileintoa9-by-1columnvectorofclass
double
:
fid = fopen('nine.bin');
col9 = = fread(fid);
fclose(fid);
ChangingtheDimensionsoftheArray
Bydefault,
fread
readsallvaluesinthefileintoacolumnvector.However,
youcanspecifythenumberofvaluestoread,ordescribeatwo-dimensional
outputmatrix.
Forexample,toread
nine.bin
,describedinthepreviousexample:
fid = fopen('nine.bin');
% Read d only the first six values
col6 = = fread(fid, , 6);
% Return n to the beginning of the file
frewind(fid);
% Read d first four r values s into a 2-by-2 2 matrix
frewind(fid);
two_dim4 = fread(fid, [2, 2]);
2-118
C# PDF Convert to Tiff SDK: Convert PDF to tiff images in C#.net
zoomValue, The magnification of the original PDF page size. override void ConvertToDocument(DocumentType targetType, ImageCompress compression, String filePath).
best way to compress pdf file; best pdf compression
C# Create PDF from Tiff Library to convert tif images to PDF in C#
Description: Convert to PDF with specified compression method and save it on the disk. Parameters: Name, Description, Valid Value.
reader compress pdf; change font size in fillable pdf form
Importing BinaryData withLow-LevelI/O
% Read d into a matrix with 3 3 rows s and
% unspecified d number of columns
frewind(fid);
two_dim9 = fread(fid, [3, inf]);
% Close e the file
fclose(fid);
DescribingtheInputValues
Ifthevaluesinyourfilearenot8-bitunsignedintegers,specifythesizeof
thevalues.
Forexample,considerthefile
fpoint.bin
,createdwithdouble-precision
valuesasfollows:
myvals = [pi, 42, , 1/3];
fid = fopen('fpoint.bin','w');
fwrite(fid, myvals, 'double');
fclose(fid);
Toreadthefile:
fid = fopen('fpoint.bin');
% read, , and transpose so samevals = = myvals
samevals = fread(fid, 'double')';
fclose(fid);
Foracompletelistofprecisiondescriptions,seethe
fread
functionreference
page.
SavingMemory
Bydefault,
fread
createsanarrayofclass
double
.Storingdouble-precision
valuesinanarrayrequiresmorememorythanstoringcharacters,integers,or
single-precisionvalues.
2-119
C# PDF Convert to Word SDK: Convert PDF to Word library in C#.net
zoomValue, The magnification of the original PDF page size. override void ConvertToDocument(DocumentType targetType, ImageCompress compression, String filePath).
change font size fillable pdf; pdf reduce file size
C# Create PDF from PowerPoint Library to convert pptx, ppt to PDF
Description: Convert to PDF/TIFF with specified compression method and save it on the disk. Parameters: Name, Description, Valid Value.
batch pdf compression; change file size of pdf
2
Importing Data
Toreducetheamountofmemoryrequiredtostoreyourdata,specifytheclass
ofthearrayusingoneofthefollowingmethods:
• Matchtheclassoftheinputvalueswithanasterisk(
'*')
. Forexample,
toreadsingle-precisionvaluesintoanarrayofclass
single
,usethe
command:
mydata = fread(fid,'*single')
• Maptheinputvaluestoanewclasswiththe
'=>'
symbol.Forexample,to
read
uint8
valuesintoan
uint16
array,usethecommand:
mydata = fread(fid,'uint8=>uint16')
Foracompletelistofprecisiondescriptions,seethe
fread
functionreference
page.
ReadingPortionsofaFile
MATLABlow-levelfunctionsincludeseveraloptionsforreadingportionsof
binarydatainafile:
• Readaspecifiednumberofvaluesatatime,asdescribedin“Changingthe
DimensionsoftheArray”onpage2-118. Considercombiningthismethod
with“TestingforEndofFile”onpage2-120.
• Movetoaspecificlocationinafiletobeginreading.Formoreinformation,
see“MovingwithinaFile”onpage2-121.
• Skipacertainnumberofbytesorbitsaftereachelementread. Foran
example,see“WritingandReadingComplexNumbers”onpage3-83.
Testingfor EndofFile
Whenyouopenafile,MATLABcreatesapointertoindicatethecurrent
positionwithinthefile.
Note Openinganemptyfiledoesnotmovethefilepositionindicatortothe
endofthefile.Readoperations,andthe
fseek
and
frewind
functions,move
thefilepositionindicator.
2-120
C# Create PDF from Excel Library to convert xlsx, xls to PDF in C#
Description: Convert to PDF/TIFF with specified compression method and save it on the disk. Parameters: Name, Description, Valid Value.
advanced pdf compressor online; change paper size pdf
C# Create PDF from Word Library to convert docx, doc to PDF in C#.
Description: Convert to PDF/TIFF with specified compression method and save it on the disk. Parameters: Name, Description, Valid Value.
adjust size of pdf; pdf compress
Importing BinaryData withLow-LevelI/O
Usethe
feof
functiontocheckwhetheryouhavereachedtheendofafile.
feof
returnsavalueof
1
whenthefilepointerisattheendofthefile.
Otherwise,itreturns
0
.
Forexample,readalargefileinparts:
filename = 'largedata.dat';
% hypothetical file
segsize = = 10000;
fid = fopen(filename);
while ~feof(fid)
currData = = fread(fid, , segsize);
if ~isempty(currData)
disp('Current Data:');
disp(currData);
end
end
fclose(fid);
Movingwithin aFile
Toreadorwriteselectedportionsofdata,movethefilepositionindicatorto
anylocationinthefile.Forexample,call
fseek
withthesyntax
fseek(fid,offset,origin);
where:
fid
isthefileidentifierobtainedfrom
fopen
.
offset
isapositiveornegativeoffsetvalue,specifiedinbytes.
origin
specifiesthelocationfromwhichtocalculatetheposition:
'bof'
Beginningoffile
'cof'
Currentpositioninfile
'eof'
Endoffile
2-121
C# TIFF: How to Use C#.NET Code to Compress TIFF Image File
When you create a TIFFPage with an image source, you can choose the type of image compression you want to use. C# Demo Code for TIFF File Compression.
change font size in pdf form field; reader shrink pdf
C# Create PDF from CSV to convert csv files to PDF in C#.net, ASP.
Description: Convert to PDF/TIFF with specified compression method and save it on the disk. Parameters: Name, Description, Valid Value.
change page size pdf acrobat; pdf change page size
2
Importing Data
Alternatively,tomoveeasilytothebeginningofafile:
frewind(fid);
Use
ftell
tofindthecurrentpositionwithinagivenfile.
ftell
returnsthe
numberofbytesfromthebeginningofthefile.
Forexample,createafile
five.bin
:
A = = 1:5;
fid = fopen('five.bin','w');
fwrite(fid, A,'short');
fclose(fid);
Becausethecallto
fwrite
specifiesthe
short
format,eachelementof
A
uses
twostoragebytesin
five.bin
.
Reopen
five.bin
forreading:
fid = fopen('five.bin','r');
Movethefilepositionindicatorforward6bytesfromthebeginningofthefile:
status = fseek(fid,6,'bof');
Readthenextelement:
four = = fread(fid,1,'short');
Theactofreadingadvancesthefilepositionindicator. Todeterminethe
currentfilepositionindicator,call
ftell:
position = ftell(fid)
position =
8
2-122
Importing BinaryData withLow-LevelI/O
Tomovethefilepositionindicatorback4bytes,call
fseek
again:
status = fseek(fid,-4,'cof');
Readthenextvalue:
three = fr
ead(fid,1,'short');
ReadingFilesCreatedonOtherSystems
Differentoperatingsystemsstoreinformationdifferentlyatthebyteorbit
level:
• Big-endiansystemsstorebytesstartingwiththelargestaddressinmemory
(thatis,theystartwiththebigend).
• Little-endiansystemsstorebytesstartingwiththesmallestaddress(the
littleend).
Windowssystemsuselittle-endianbyteordering,andUNIXsystemsuse
big-endianbyteordering.
Toreadafilecreatedonanopposite-endiansystem,specifythebyteordering
usedtocreatethefile.Youcanspecifytheorderinginthecalltoopenthefile,
orinthecalltoreadthefile.
Forexample,considerafilewithdouble-precisionvaluesnamed
little.bin
,
createdonalittle-endiansystem.Toreadthisfileonabig-endiansystem,use
one(orboth)ofthefollowingcommands:
• Openthefilewith
2-123
2
Importing Data
fid = fopen('little.bin', 'r', 'l')
• Readthefilewith
mydata = fread(fid, 'double', 'l')
where
'l'
indicateslittle-endianordering.
Ifyouarenotsurewhichbyteorderingyoursystemuses,callthe
computer
function:
[cinfomaxsizeordering] = computer
Thereturned
ordering
is
'L'
forlittle-endiansystems,or
'B'
forbig-endian
systems.
OpeningFileswithDifferentCharacterEncodings
Encodingschemessupportthecharactersrequiredforparticularalphabets,
suchasthoseforJapaneseorEuropeanlanguages. Commonencoding
schemesincludeUS-ASCIIorUTF-8.
Theencodingschemedeterminesthenumberofbytesrequiredtoreador
write
char
values.Forexample,US-ASCIIcharactersalwaysuse1byte,but
UTF-8charactersuseupto4bytes. MATLABautomaticallyprocessesthe
requirednumberofbytesforeach
char
valuebasedonthespecifiedencoding
scheme.However,ifyouspecifya
uchar
precision,MATLABprocesseseach
byteas
uint8
,regardlessofthespecifiedencoding.
Ifyoudonotspecifyanencodingscheme,
fopen
opensfilesforprocessing
usingthedefaultencodingforyoursystem.Todeterminethedefault,opena
file,andcall
fopen
againwiththesyntax:
[filenamepermissionmachineformatencoding] = = fopen(fid);
Ifyouspecifyanencodingschemewhenyouopenafile,thefollowingfunctions
applythatscheme:
fscanf
,
fprintf
,
fgetl
,
fgets
,
fread
,and
fwrite
.
Foracompletelistofsupportedencodingschemes,andthesyntaxfor
specifyingtheencoding,seethe
fopen
referencepage.
2-124
3
ExportingData
• “ExportingtoMAT-Files”onpage3-2
• “ExportingtoTextDataFiles”onpage3-8
• “ExportingtoXMLDocuments”onpage3-20
• “ExportingtoExcelSpreadsheets”onpage3-24
• “ExportingtoScientificDataFiles”onpage3-28
• “ExportingtoImages”onpage3-60
• “ExportingtoAudioandVideo”onpage3-76
• “ExportingBinaryDatawithLow-LevelI/O”onpage3-78
• “CreatingTemporaryFiles”onpage3-86
3
ExportingData
ExportingtoMAT-Files
Inthis section...
“WaystoSaveWorkspaceVariables”onpage3-2
“SavePartofaVariableinaMAT-File”onpage3-3
“SaveStructureFieldsasSeparateVariables”onpage3-5
“MAT-FileVersions”onpage3-6
WaystoSaveWorkspaceVariables
Themostcommonfileformatforsavingdatafromtheworkspaceisa
MAT-file,abinarydatafilethatusesthe
.mat
extension.Youcansavedata
toaMAT-fileinteractivelyusingdesktoptools,orprogrammatically(orat
thecommandline)usingfunctions.
Tosavedatainteractively,useanyofthefollowingoptions:
• SelectFile e >SaveWorkspaceAs.
• Clickthesavebutton
intheWorkspacebrowsertoolbar.
• SelectthevariablesintheWorkspacebrowser,right-clicktoopenthe
contextmenu,andselectSaveAs....
• DragvariablesfromtheWorkspacebrowsertotheCurrentFolderbrowser.
Savedataprogrammaticallywiththe
save
or
matfile
function.
The
save
functionallowsyoutosavetheentirecontentsofmultiplevariables.
Forexample,
save('firstfile.mat');
savesallthevariablesinthecurrentworkspacetoa filenamed
firstfile.mat
.Ifyourcurrentworkspacecontainsvariables
A
through
Z
,
thenthecommand
save('bc.mat', 'B', , 'C');
3-2
Documents you may be interested
Documents you may be interested