writedictionary
Description
writedictionary(
specifies options using one or more name-value arguments. For example, you can export the
contents of the input dictionary as a JSON file regardless of the file extension specified
in d
,filename
,Name=Value
)filename
by calling
writedictionary(d,filename,FileType="json")
.
Examples
Write Dictionary to File
Create a dictionary and write it to a JSON file.
wheels = [1 2 3]; names = ["Unicycle" "Bicycle" "Tricycle"]; d = dictionary(names,wheels); writedictionary(d,"vehicles.json");
Display the created file.
type vehicles.json
{ "Unicycle": 1.0, "Bicycle": 2.0, "Tricycle": 3.0 }
Write Dictionary to File Without Indentation
Create a dictionary and write it to a JSON file without indentation.
wheels = [1 2 3]; names = ["Unicycle" "Bicycle" "Tricycle"]; d = dictionary(names,wheels); writedictionary(d,"vehicles.json",PrettyPrint=false);
Display the created file.
type vehicles.json
{"Unicycle":1.0,"Bicycle":2.0,"Tricycle":3.0}
Input Arguments
d
— Input dictionary
MATLAB® dictionary
Input dictionary, specified as a MATLAB dictionary. A dictionary is a map that stores data as values, which can be accessed using corresponding unique
keys. Each pair of keys and values is an entry. For more information on dictionaries, see dictionary
.
When creating JSON files, writedictionary
writes missing values
as null
values and NaN
values as
null
or NaN
values depending on the PreserveInfAndNaN
name-value argument.
filename
— Name of file to write
string scalar | character vector
Name of the file to write, specified as a string scalar or character vector. If
filename
does not exist, then the writing function creates the
file. If filename
is an existing file, then the function overwrites
it.
Depending on the location you are writing to, filename
can take
one of these forms.
Location | Form | ||||||||
---|---|---|---|---|---|---|---|---|---|
Current folder | To write to the current folder, specify the name of the file in
Example:
| ||||||||
Other folders | To write to a folder that is not the current folder, specify the
full or relative pathname in
Example:
Example:
| ||||||||
Remote location | To write to a remote location, specify a uniform resource locator (URL) of the form:
Based on the remote location,
For more information, see Work with Remote Data. Example:
|
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: writedictionary(d,filename,PreserveInfandNaN=false)
writes
all Inf
and NaN
values as JSON null
values.
FileType
— Type of file
"auto"
(default) | "json"
Type of file, specified as one of these values:
"auto"
— Automatically detect the file format to write from the extension specified infilename
."json"
— Export the contents of the dictionary as a JSON file, regardless of the file extension specified infilename
.
If you specify a file extension in filename
that is not
.json
, you can specify FileType
as
"json"
to write the contents of the input dictionary as
JSON.
PrettyPrint
— Indent text
true
or 1
(default) | false
or 0
Indent text in the output file, specified as a numeric or logical
1
(true
) or 0
(false
). If you specify the argument as true
,
then writedictionary
writes the JSON text with an indentation of
four spaces. If you specify the argument as false
, then
writedictionary
writes the JSON text without spaces or line
breaks.
PreserveInfAndNaN
— Preserve Inf
and NaN
values in JSON file
true
or 1
(default) | false
or 0
Preserve Inf
and NaN
values in the output
JSON file, specified as a numeric or logical 1
(true
) or 0
(false
).
Specify this argument as false
to write all Inf
and NaN
values as JSON null
values.
Version History
Introduced in R2024b
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)