Hot answers tagged import
13
You can create styled cells with a custom CellEvaluationFunction to do this (similar to this R cell answer). As a simple example:
URLCell[] := Module[{},
CellPrint@Cell[TextData[""], "Program",
Evaluatable -> True,
CellEvaluationFunction -> (Import@First@FrontEndExecute[FrontEnd`ExportPacket[Cell[#], "InputText"]] &),
...
7
At OP's behest:
The easiest approach to see the mesh lines is to remove the EdgeForm[] instruction that causes them not to appear. For instance,
DeleteCases[Import["ExampleData/wrench.obj.gz"], _EdgeForm, ∞]
As SEngstrom suggests, you can also use a replacement rule. If, for instance, you want a thick gray mesh, here's what you can do:
...
6
What about this:
Import["ExampleData/wrench.obj.gz", "PolygonObjects"] // Graphics3D
You can use the FaceForm[None] trick as shown by @J.M. here just as well if you only want the wireframe looks.
6
Import is very well able to handle this format. As a demonstration I use its nephew ImportString to deal with the few lines from your example:
data =
ImportString[
"28/04/2013 20:01:36.18 2.5013E-2 W
28/04/2013 20:01:36.26 2.5013E-2 W
28/04/2013 20:01:36.32 2.5013E-2 W
28/04/2013 20:01:36.35 2.5011E-2 W
28/04/2013 20:01:36.48 ...
6
This is a bug due to the fact that URLFetch is dealing incorrectly with line breaks. Here's a workaround:
in1 = URLFetch[
"http://blog.wolfram.com/wp-content/uploads/2008/06/se-30.jpg",
"ContentData"
];
ImportString[FromCharacterCode[in1]]
You can compare this technique to the other as follows:
in2 = ToCharacterCode[URLFetch[
...
5
I recently had a similar task decoding 12-bit binary 24-channel electroencephalogram data files and found that bit shifting and masking with BitAnd are the way to go. The specific approach depends on file structure, endianness and channel number. In the case of this 'format 212' binary data file containing two interlaced signals, the following procedure ...
5
If I am not mistaken the 212 format is storing two signals and each 3-byte group provides a 12-bit reading of that pair. Importing with Byte isn't too bad for this set:
data = Import["http://physionet.org/physiobank/database/mitdb/100.dat", "Byte"] ~ Partition ~ 3
Then two helpers to parse it:
twosComplement[a_, n_] := If[a < 2^(n - 1), a, a - 2^n]
...
4
Since you requested performance I would avoid Import and DateList and use ReadList and AbsoluteTime.
format = {Number, Character, Number, Character, Number, Number,
Character, Number, Character, Number, Number, Word};
data = {AbsoluteTime[{#5, #3, #1, #6, #8, #10}], ##11} & @@@ ReadList["data.txt", format];
"data.txt" is of course your data ...
4
Surely not the fastest or ideal solution, but this should work (it assumes that you have two numbers in each row):
randomline[str_InputStream, num_] := (
SetStreamPosition[ str, 0];
Skip[str, Record, num-1];
Read[str, {Number, Number}])
str = OpenRead["data.txt"];
Now, if you have 2000 lines and want 100 random samples:
Map[randomline[str, #] ...
3
Using BlockStream from this answer and the techniques from this answer, this is how I would load an LTSpice using the sample as a template:
ClearAll[toRule, skip];
Options[toRule] = {Trim -> True};
toRule[s_String, opts : OptionsPattern[]] := toRule[s, ":", opts]
toRule[s_String, del_, opts : OptionsPattern[]] :=
Rule @@ If[OptionValue[Trim], ...
3
The following works :
fileFullName = (*to be defined by you*)
page = 1;
ti00 = Import[fileFullName, "Text"] //
StringReplace[#, {Except[StartOfString] ~~ "Title" ->
"TitleTitle"}] & //
StringSplit[#,
Shortest["Title: " ~~ x : ___ ~~ "Title"] :>
"Title: " <> x] & //
(#[[page]] &) //
...
3
Lets do an exercise importing and exporting JPEG data. First let's create a tiny single pixel JPEG file:
In[1]:= Export["~/Desktop/minitest.jpg", Image[{{0}}]]
Now lets import the binary data that we have just created:
In[2]:= Import["/Users/gdelfino/Desktop/minitest.jpg", "Binary"]
Out[2]:= {255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 1, 0, 72, ...
3
Your binarydata seems to be a string with hexadecimal digits. Converting this using FromCharacterCode yields nonsense, of course.
It can be solved in a few steps:
0) The string:
imageString =
"FFD8FFE000104A46494600010201006000600000FFEE000E41646F6265006400000\
00001FFE1135D4578696600004D4D002A0000000800070132000200000014000000620\
...
3
This might work... however, it is very inefficient.
Slightly faster using Import instead of BinaryReadList:
binary= Partition[Import["http://physionet.org/physiobank/database/mitdb/100.dat", "Bit"], 12];
twoscomplement[bin_] :=
If[First@bin != 1,
FromDigits[Rest@bin, 2],
-FromDigits[Rest@bin /. {1 -> 0, 0 -> 1}, 2] - 1
]
output = ...
3
You can wrap your Skip and Readlist in a Module and make it a function:
importfile[name_] := Module[{strm, mydata},
strm = OpenRead[name];
Skip[strm, Record, 38, NullRecords -> True];
mydata = ReadList[strm, Number, 7500*3, RecordLists -> True];
Close[strm];
mydata
]
importfile@"f1-Oxs_TimeDriver-Magnetization-000000-0000028.omf"
This ...
3
Via @chuy and @rcollyer with sequential skipping and reading of records, and without backtracking.
(
If[# != 1, Skip[s, Record, # - 1], Null];
ReadList[s, Number, 2]) & /@ Differences[p~Prepend~0]
List p of lines you want to read, sorted; eg. some random ten from a hundred:
p = Sort[RandomSample[Range[100], 10]];
Your file opened as a stream, ...
2
A simplistic,if slightly inefficient, solution might be:
data = Import[#,"Table"][[39;;-3]]&/@Filenames[];
Other options might involve, 'Cases', 'Choices', Drop, Take.
For example:
data = Drop[Drop[Import[#,"Table"],38],-2]&/@Filenames[];
If you are using a Linux derivative or something like cygwin on Windows, then this is an efficient ...
1
Good for you folks for doing it in Mathematica! But there is a good argument for making this kind of improvement to your system at a lower, system-wide level (no matter what XKCD says). If, for example, you're constantly wanting to insert the URL of the current web page into another document, it makes sense (to me) to make this happen for all applications ...
1
Why not just make a palette button? It's simpler and more reusable than a custom cell style.
Simple way:
PasteButton[Defer[Import["\[SelectionPlaceholder]"]]]
More complicated way:
Button["Paste URL Import",
Module[{data, strip},
data = NotebookGet[ClipboardNotebook[]][[1, 1, 1]];
If[Head[data] === String,
NotebookWrite[InputNotebook[], ...
1
A way to do this is to use rules. You have four things that can happen, so you can write four rules to substitute.
data//. {"0\t0" -> {0, 0}, "1\t0" -> {1, 0}, "0\t1" -> {0, 1}, "1\t1" -> {1, 1}}
Then you need to reshape the data. Flatten the output of the above and then partition it into the matrices of the correct size:
data2 = ...
1
ReadList thinks your file has lists of 4, not 5, elements; it views the minutes and the "m" as a single element. The following will import the lists.
dd = ReadList["/tmp/data8.txt", {Word, Word, Number, Character}]
{{"Apr-09", "-1m", 47, "s"}, {"Apr-11", "-1m", 15, "s"}, {"Apr-12",
"-0m", 59, "s"}, {"Apr-13", "-0m", 44, "s"}, {"Apr-14", "-0m", ...
1
Another variant..
twoscomplement[bin_] := If[First@bin != 1,
FromDigits[bin, 2], BitOr[FromDigits[bin, 2], -2^12]]
or to be safe..
twoscomplement[bin_List /; Length[bin] == 12] := If[First@bin != 1,
FromDigits[bin, 2], BitOr[FromDigits[bin, 2], -2^12]]
1
By importing that URL you are asking Mathematica to do something overly complex, since you are asking it to parse the page source code for the Google Docs interface. Even if Mathematica could process it, all you would have would be symbolic XML representing that HTML code, not the values that you want.
It should however work to import the data if you export ...
1
You'll want to use Algorithm R for reservoir sampling to get a truly random sample (as opposed to periodic) with only one scan of the input and no need to know the length of the input in advance. Basically, you take the first items and then randomly replace them with decreasing frequency as you complete the scan.
sampleSize = 100;
stream = ...
1
First Import the file. Depending on the structure of your .txt file you can use the second argument ("Data" and "List" are useful choices). Remove the parts you don't need (in this case the ------) and split the resulting strings. An empty list shows up from all the dashed, you can simply remove this using /. {} -> Sequence[].
foo = (StringSplit /@ ...
1
If the file is very large and importing it takes too much memory, you can use external tools to cut the columns you are interested in. You can often call these tools directly from Mathematica, for example
ReadList["!awk '{print $1, $4}' file.txt", {Number, Number}]
This will use awk to cut columns 1 and 4. ReadList, with an explicit specification of the ...
Only top voted, non community-wiki answers of a minimum length are eligible




