I'm very new to Mathematica. I want to use it as a data source for gnuplot (I know Mathematica can plot too), it uses a file format with data in columns and a space between each column on each row. Like this:
x y
1 123
2 234
4 456
etc.
I've come as far as to create this expression:
{CountryData["G8"], CountryData[#, "GDP"] & /# CountryData["G8"]} // Transpose // Grid
This creates a table just like I want it. Now, how can I export this to a file not as matrix but as a table like it appears in Mathematica?
Your CountryData usage can be streamlined a bit using the map operator /#:
{#, CountryData[#, "GDP"]} & /# CountryData["G8"],
Combining this with Export you get this:
Export[
"C:\\Users\\Sjoerd\\Desktop\\tabel.txt",
{#, CountryData[#, "GDP"]} & /# CountryData["G8"],
"Table",
"FieldSeparators" -> " "
]
Replace the above file path with something appropriate for your situation.
Mathematica supports a wide range of Export formats. Something like Export["mytable.csv",nameofexpression] should do the trick, Export["file.dat",nameofexpression,"Table"] for space /tab delimited.
This tutorial should help.
Related
Say that I have these data:
sysuse auto2, clear
gen name = substr(make, 1,3)
drop if missing(rep78)
gen n = 1
collapse (mean) mpg (sum) (n), by(name)
replace name = "a b: c" if _n==1
I would like to export them to an .rtf (.tex, etc.) file directly from the data using esttab or estout. Is this possible? The key reason I want to do this is that I want to be able to preserve the spaces in the row names. And it would be nice to able to have the option to have commas after for 1,000's.
One partial approach is to save the data to a matrix, then export the matrix using esttab, but can I need this extra step?
mkmat mpg n, matrix(mat) rownames(name)
esttab matrix(mat)
A problem with this is that it replaces the spaces in the row names with _'s. Another problem is that if any of the rownames (from the variable name) are :, then this creates the category in the output. Is there another solution? Either to directly export from the data or possibly to somehow save the data in an estimation?
Instead of using collapse, you can calculate means and counts directly with estpost tabstat, statistics(mean count) by(). You can then use esttab to export the results.
If you really want to create a dataset first, you can still use estpost tabstat. This appears to work for your dataset:
estpost tabstat mpg n, by(name) nototal
esttab, cells("mpg n") varlabels(`e(labels)') noobs nonumber nomtitle
If you want to have "a b: c" on top again you can use the order option of esttab.
I am doing an iterative calculation on maple and I want to store the resulting data (which comes in a column matrix) from each iteration into a specific column of an Excel file. For example, my data is
mydat||1:= <<11,12,13,14>>:
mydat||2:= <<21,22,23,24>>:
mydat||3:= <<31,32,33,34>>:
and so on.
I am trying to export each of them into an excel file and I want each data to be stored in consecutive columns of the same excel file. For example, mydat||1 goes to column A, mydat||2 goes to column B and so on. I tried something like following.
with(ExcelTools):
for k from 1 to 3 do
Export(mydat||k, "data.xlsx", "Sheet1", "A:C"): #The problem is selecting the range.
end do:
How do I select the range appropriately here? Is there any other method to export the data and store in the way that I explained above?
There are couple of ways to do this. The easiest is certainly to put all of your data into one data structure and then export that. For example:
mydat1:= <<11,12,13,14>>:
mydat2:= <<21,22,23,24>>:
mydat3:= <<31,32,33,34>>:
mydata := Matrix( < mydat1 | mydat2 | mydat3 > );
This stores your data in a Matrix where mydat1 is the first column, mydat2 is the second column, etc. With the data in this form, either ExcelTools:-Export or the more generic Export command will work:
ExcelTools:-Export( data, "data.xlsx" );
Export( "data.xlsx", data );
Now since you mention that you are doing an iterative calculation, you may want to write the results out column by column. Here's another method that doesn't involve the creation of another data structure to house the results. This does assume that the data in mydat"i" has been created before the loop.
for i to 3 do
ExcelTools:-Export( cat(`mydat`,i), "data.xlsx", 1, ["A1","B1","C1"][i] );
end do;
If you want to write the data out to a file as you are building it, then just do the Export call after the creation of each of the columns, i.e.
ExcelTools:-Export( mydat1, "data.xlsx", 1, "A1" );
Note that I removed the "||" characters. These are used in Maple for concatenation and caused some issues with the second method.
I have a number of files (having 10 columns each) with following order:
file_001.txt, file_002.txt, file_003_txt,
file_021.txt, file_023.txt, file_023.txt,
file_041.txt, file_042.txt, file_043.txt,
file_061.txt, file_062.txt, file_063.txt,
file_081.txt, file_082.txt, file_083.txt,
I would like to plot each file with different line. e.g. using 1:2, using 1:3, using 1:5, using 1:8. I can not able to make a loop to call different columns. My following script is not working for k field
plot for [k=2, 3, 5, 8] for [j=0:8:2] for [i=1:3] 'file_0'.j.i.'.txt' u 1:k;
Use for [k in "2 3 5 8"] if you have a list rather than a range.
If j can be > 9, you should set up a function
fname(j,i) = sprintf("name%02.f%.f",j,i)
to get proper file names.
Format string "%02.f" means float (f), no digits after the comma (.), minimum two postions (2), fill empty space with zeroes.
print fname(2,3)
name023
print fname(13,3)
name133
print fname(113,3)
name1133
These are libc format strings, they are not documented inside the gnuplot docs, but there are many sources in the web.
I'm new to Mathematica, trying to ListPlot set of data having time in the first column and values in the columns 2 and 3.
t = Import["/.../time_deltas.csv", "CSV"]
{{11.1694,0.,0}, {11.1697,0.000275,0}, {11.1702,0.000495,0},
{11.1702,0.000028,0}, {11.1702,1.*10^-6,0}, {11.1702,0.000033,0},
{11.1707,0.000502,0}, {11.171,0.000314,0}, {11.171,4.*10^-6,0},
{11.1711,0.000025,0}, {25.8519,0.000029,1}, {25.852,0.000049,1},
{25.852,0.000032,1}, {25.8521,0.000031,1}, {25.8524,0.000388,1},
{25.8524,1.*10^-6,1}, {25.8525,0.000051,1}, {25.8543,0.001852,1},
{25.9342,0.079813,0},{25.9391,0.004914,0}}
Plotting a single point I expected that:
ListPlot[{t[[1,2,1]],t[[1,2,2]]}]
Would plot a single point with x=11.169662 and y=0.000275. Instead it plots two points with x=1,2 and y=11.169662, 0.000275.
What am I doing wrong ?
You need to group the data into x-y pairs for each list plot, e.g.
u = {{#1, #2}, {#1, #3}} & ### t;
v = Transpose[u];
GraphicsColumn[{ListPlot[v[[1]]], ListPlot[v[[2]]]}]
v[[1]] shows the data pairs you expect:
{{11.1694, 0.}, {11.1697, 0.000275}, {11.1702, 0.000495}, ... }
I have two different variables which are stored as cell arrays. I try to open text file and store these variables as two column arrays. Below is my code, i used \t to seperate x and y data, but in the output file, the x data is written first which is followed by the y data. How can I obtain two column array in the text file?
for j=1:size(data1,2)
file1=['dir\' file(j,1).name];
f1{j}=fopen(file1,'a+')
fprintf(f1{j},'%7.3f\t%20.10f\n',x{1,j}',y{1,j});
fclose(f1{j});
end
Thanks in advance!
You can use dlmwrite as well to accomplish this for numeric data:
x = [1;2;3]; y = [4;5;6]; % two column vectors
dlmwrite('foo.dat',{x,y},'Delimiter','\t')
This produces the output:
1 4
2 5
3 6
Use a MATLAB table if you have R2013b or beyond:
data1 = {'a','b','c'}'
data2 = {1, 2, 3}'
t = table(data1, data2)
writetable(t, 'data.csv')
More info here.