Which of the following commands can be used to read the entire contents of a file as a string using the file object fob?
Save workspace variables to file Show
SyntaxDescriptionexample save(filename) saves all variables from the current workspace in a MATLAB® formatted binary file (MAT-file) called filename. If filename exists, save overwrites the file. example save(filename,variables,fmt) saves in the file format specified by fmt. The variables argument is optional. If you do not specify variables, the save function saves all variables in the workspace. example save(filename,variables,version,'-nocompression') saves the variables to the MAT-file without compression. The '-nocompression' flag only supports MAT-file Version 7 (default) and Version 7.3. Therefore, you must specify version as '-v7' or '-v7.3'. The variables argument is optional. example save(filename,variables,'-append') adds new variables to an existing file. If a variable already exists in a MAT-file, then save overwrites it with the value in the workspace. For ASCII files, '-append' adds data to the end of the file. To append to a Version 6 MAT-file, you must also include '-v6' as an input argument. example save(filename,variables,'-append','-nocompression') adds new variables to an existing file without compression. The existing file must be a MAT-file Version 7 (default) or 7.3. example save filename is the command form of the syntax. Command form requires fewer special characters. You do not need to type parentheses or enclose the input in single or double quotes. Separate inputs with spaces instead of commas. For example, to save a file named test.mat, these statements are equivalent: save test.mat % command form save('test.mat') % function form You can include any of the inputs described in previous syntaxes. For example, to save the variable named X: save test.mat X % command form save('test.mat','X') % function form Do not use command form when any of the inputs, such as filename, are variables or strings. Examples
collapse all Save All Workspace Variables to MAT-FileSave all variables from the workspace in a binary MAT-file, test.mat. If filename is a variable, use function syntax. filename = 'test.mat'; save(filename) Otherwise, you also can use command syntax. Remove the variables from the workspace, and then retrieve the data with the load function. Save Specific Variables to MAT-FileCreate and save two variables, p and q, to a file called pqfile.mat. p = rand(1,10); q = ones(10); save('pqfile.mat','p','q') MATLAB® saves the variables to the file, pqfile.mat, in the current folder. You also can use command syntax to save the variables, p and q. Save Data to ASCII FileCreate two variables, save them to an ASCII file, and then view the contents of the file. p = rand(1,10); q = ones(10); save('pqfile.txt','p','q','-ascii') type('pqfile.txt') The type function displays the contents of the file. Alternatively, use command syntax for the save operation. save pqfile.txt p q -ascii Save Structure Fields as Individual VariablesCreate a structure, s1, that contains three fields, a, b, and c. s1.a = 12.7; s1.b = {'abc',[4 5; 6 7]}; s1.c = 'Hello!'; Save the fields of structure s1 as individual variables in a file called newstruct.mat. save('newstruct.mat','-struct','s1'); Check the contents of the file using the whos function. disp('Contents of newstruct.mat:') Contents of newstruct.mat: whos('-file','newstruct.mat') Name Size Bytes Class Attributes a 1x1 8 double b 1x2 246 cell c 1x6 12 char Save Variables to Version 7.3 MAT-FileCreate two variables and save them to a Version 7.3 MAT-file called example.mat. A = rand(5); B = magic(10); save('example.mat','A','B','-v7.3') You also can use command syntax for the save operation. save example.mat A B -v7.3 Save Variables to MAT-File Without CompressionCreate two variables and save them, without compression, to a Version 7 or 7.3 MAT-file called myFile.mat. A = rand(5); B = magic(10); save('myFile.mat','A','B','-v7.3','-nocompression') Alternatively, use the command syntax for the save operation. save myFile.mat A B -v7.3 -nocompression The '-nocompression' flag facilitates a faster save for those variables that are larger than 2 GB or those that do not benefit from compression. Append Variable to MAT-FileSave two variables to a MAT-file. Then, append a third variable to the same file. p = rand(1,10); q = ones(10); save('test.mat','p','q') View the contents of the MAT-file. Name Size Bytes Class Attributes p 1x10 80 double q 10x10 800 double Create a new variable, a, and append it to the MAT-file. a = 50; save('test.mat','a','-append') View the contents of the MAT-file. Name Size Bytes Class Attributes a 1x1 8 double p 1x10 80 double q 10x10 800 double The variable, a, is appended to test.mat, without overwriting the previous variables, p and q. Note To append to a Version 6 MAT-file, specify both '-v6' and '-append'. For example, to save variable a to the file, test.mat, call: save('test.mat','a','-v6','-append') Append Variable to MAT-File Without CompressionSave two variables to a MAT-file. Then, append a third variable, without compression, to the same file. Create two variables A and B and save them to a MAT-file Version 7 or 7.3. By default, the save function compresses variables A and B before saving them to myFile.mat. A = rand(5); B = magic(10); save('myFile.mat','A','B','-v7.3') View the contents of the MAT-file. whos('-file','myFile.mat') Name Size Bytes Class Attributes A 5x5 200 double B 10x10 800 double Create a new variable C and append it, without compression, to myFile.mat. C = 5; save('myFile.mat','C','-append','-nocompression') View the contents of the MAT-file. whos('-file','myFile.mat') Name Size Bytes Class Attributes A 5x5 200 double B 10x10 800 double C 1x1 8 double Input Argumentscollapse all filename — Name of file 'matlab.mat' (default) | character vector | string scalarName of file, specified as a character vector or string scalar. If you do not specify filename, the save function saves to a file named matlab.mat. If filename has no extension (that is, no period followed by text), and the value of format is not specified, then MATLAB appends .mat. If filename does not include a full path, MATLAB saves to the current folder. You must have permission to write to the file. When using the command form of save, you do not need to enclose the input in single quotes. However, if filename contains a space, you must enclose the argument in single quotes. For example, save 'filename withspace.mat'. Note Do not use command form when filename is a string. To save workspace variables to a MAT-file in a remote location, specify filename as a uniform resource locator (URL) of this form: scheme_name://path_to_file/my_file.mat Based on your remote location, scheme_name can be one of the values in this table.
The save function only supports saving version 7.3 MAT-files to remote locations. For more information on setting up MATLAB to access your online storage service, see Work with Remote Data. Example: 'myFile.mat' Example: 's3://bucketname/path_to_file/my_file.mat' variables — Names of variables to save character vector | string scalarNames of variables to save, specified as character vectors or string scalars. When using the command form of save, you do not need to enclose the input in single quotes. Note Do not use command form when variables is a string. variables can be in one of the following forms.
fmt — File format '-mat' (default) | '-ascii' | '-ascii','-tabs' | '-ascii','-double' | '-ascii','-double','-tabs'File format, specified as one of the following. When using the command form of save, you do not need to enclose the input in single or double quotes, for example, save myFile.txt -ascii -tabs.
For MAT-files, data saved on one machine and loaded on another machine retains as much accuracy and range as the different machine floating-point formats allow. Use one of the text formats to save MATLAB numeric values to text files. In this case:
If you specify a text format and any variable is a two-dimensional character array, then MATLAB translates characters to their corresponding internal ASCII codes. For example, 'abc' appears in a text file as: 9.7000000e+001 9.8000000e+001 9.9000000e+001 When saving to a remote location, save only supports specifying fmt as '-mat'. Data Types: char | string version — MAT-file version '-v7' (default) | '-v7.3' | '-v6' | '-v4'MAT-file version, specified as one of the following. When using the command form of save, you do not need to enclose the input in single or double quotes.
If any data items require features that the specified version does not support, MATLAB does not save those items and issues a warning. You cannot specify a version later than your current version of MATLAB software. Note Version 7.3 MAT-files use an HDF5 based format that requires some overhead storage to describe the contents of the file. For cell arrays, structure arrays, or other containers that can store heterogeneous data types, Version 7.3 MAT-files are sometimes larger than Version 7 MAT-files. To view or set the default version for MAT-files, go to the Home tab and in the Environment section, click Preferences. Select > > and then choose a MAT-file save format option. Data Types: char | string Limitations
Tips
Version HistoryIntroduced before R2006a Which of the following commands can be used to read the entire contents of a file as string using the File object?fgets()– This function is used to read strings from files.
Which command is used to read the entire content of the file as a string?readlines() -> [str] : Read all lines into a list of strings. fileObj. read() -> str : Read the entire file into a string.
Which of the following commands can be used to read the entire contents of a file as a string using the file object f >?Answer: The correct option for the command to read the entire contents of a file as string using the object
Which method is used to display the entire contents of the file?Explanation: read function is used to read all the lines in a file.
|