Message Boards Message Boards

1
|
4414 Views
|
16 Replies
|
7 Total Likes
View groups...
Share
Share this post:

Export data (graphs) in excel/txt/csv format?

Posted 1 year ago

Hello,

I want to export the 2 bode plot graphs either in a .txt file (so that I can then import the .txt file in excel and redo the graph in excel as it looks like the two graphs in Mathematica), or export directly the two graphs from Mathematica in excel format (.xlsx or .csv) and then to be able to plot the graphs in excel.

H = TransferFunctionModel[1/s, s]
Result = 
 BodePlot[H[I*2*\[Pi]*freq], {freq, 10^-3, 10^6}, 
  PlotRange -> {{-90, 60}, {-180, 0}}]
Export["C:\\Desktop\\test.xlsx", Result]

enter image description here

I already tried a variant with that Export above, but when I open Excel I don't really know how I could plot the 2 graphs in Excel considering how the data are exported, so that I can redo the 2 graphs from Mathematics in excel.

The path where the txt/excel/CSV file is exported from my example (C:\ Desktop \ test.xlsx) can be different, it doesn't matter.

enter image description here

Attached are Mathematica 13.2 Notebook file and the exported excel file (test.xlsx).
Thank you.

Attachments:
POSTED BY: Cornel B.
16 Replies
Posted 1 year ago
GraphicsRow[{result, 
      ListLogLinearPlot[FirstGraphPlotPoints1, Joined -> True, 
       Frame -> True, GridLines -> Automatic, 
       PlotRange -> {{Automatic, Automatic}, {-90, 50}}, 
       FrameTicks -> {{Automatic, 
          Automatic}, {Table[{10^i, Superscript[10, i]}, {i, -3, 6}], 
          None}}], 
      Show[result, 
       ListLogLinearPlot[FirstGraphPlotPoints1, Joined -> True]]}]

enter image description here

POSTED BY: Cornel B.
Posted 1 year ago

Here is somethin misterious:

h = TransferFunctionModel[1/s, s]]
result = 
  BodePlot[h[I*2*\[Pi]*freq], {freq, 10^-3, 10^6}, 
   PlotRange -> {{-90, 50}}, PlotLayout -> "Magnitude", 
   ScalingFunctions -> {"Log10", "dB"}, 
   FrameLabel -> {HoldForm[Text[Frequency[Hz]]], 
     HoldForm[Text[Magnitude[dB]]]}, GridLines -> Automatic, 
   PlotStyle -> Thickness[0.005], 
   FrameTicks -> {{Automatic, 
      Automatic}, {Table[{10^i, Superscript[10, i]}, {i, -3, 6}], 
      None}}];
points1 = Cases[result, Line[data_] :> data, Infinity][[1]];
FirstGraphPlotPoints1 = 
  Transpose[
   Join[{10^Transpose[points1][[1]]}, {Transpose[points1][[2]]}]];
Export["C:\\Desktop\\FirstGraphPlotPoints1.xlsx", \
FirstGraphPlotPoints1]
test = Import["C:\\Desktop\\FirstGraphPlotPoints1.xlsx"];
test[[1]] - FirstGraphPlotPoints1

enter image description here

ListLogLinearPlot[FirstGraphPlotPoints1, Joined -> True, 
 GridLines -> Automatic, Frame -> True, 
 PlotRange -> {{Automatic, Automatic}, {-90, 50}}, 
 FrameTicks -> {{Automatic, 
    Automatic}, {Table[{10^i, Superscript[10, i]}, {i, -3, 6}], 
    None}}]

enter image description here

result

enter image description here

They're the same, but when I put them together:

Show[result, ListLogLinearPlot[FirstGraphPlotPoints1, Joined -> True]]

enter image description here

Something mysterious happens when I put the 2 graphs together. Individually they are identical (you can look at the 2 graphs individually), but together they no longer seem identical. There is a problem with this show function. What do you think that happens here at show function?

Mathematica 13.2 Notebook file attached.

POSTED BY: Cornel B.
Posted 1 year ago

Now,

h = TransferFunctionModel[1/s, s]
result = 
 BodePlot[h[I*2*\[Pi]*freq], {freq, 10^-3, 10^6}, 
  PlotRange -> {{-90, 60}}, PlotLayout -> "Magnitude", 
  ScalingFunctions -> {"Log10", "dB"}, 
  FrameLabel -> {HoldForm[Text[Frequency[Hz]]], 
    HoldForm[Text[Magnitude[dB]]]}, GridLines -> Automatic, 
  PlotStyle -> Thickness[0.005], 
  FrameTicks -> {{Automatic, 
     Automatic}, {Table[{10^i, Superscript[10, i]}, {i, -3, 5}], 
     None}}]

enter image description here

ListLogLinearPlot[test[[1]], Joined -> True]

enter image description here

Show[result, ListLogLinearPlot[test[[1]], Joined -> True]]

enter image description here

Why aren't the 2 graphs placed on top of each other? (i.e. should they be the same? - this is how the 2 graphs should be, the same)

Each graph taken separately looks the same, but when I put them together it doesn't look the same...

Attachments:
POSTED BY: Cornel B.
Posted 1 year ago

Check out ListLogLinearPlot

ListLogLinearPlot[test[[1]], Joined -> True]

enter image description here

POSTED BY: Hans Milton
Posted 1 year ago

Hans Milton Thankkk you!!!!

POSTED BY: Cornel B.
Posted 1 year ago

Mathcad: enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Mathematica:

enter image description here

enter image description here

The graph in Mathematica is the equivalent of graph 2 in Mathcad, with a non-logarithmic x-axis, off (non-Log10). The idea is how can it be done in Mathematica, having available the points from the attached excel FirstGraphPlot.xlsx, graph 1 from Mathcad to be replicated with the logarithmic x-axis, on (Log10)?

The idea is that the x-axis in the Mathematica plot is not the Log10 axis, but the linear axis. How can the x-axis be changed to the Log10 axis for the respective points on the x-axis?

(In Mathcad I also plotted the Bode diagram for h just for verification.)

Mathematica 13.2 Notebook file attached. Excel FirstGraphPlot.xlsx file attached.

POSTED BY: Cornel B.
Posted 1 year ago

I don't know how to do that without the additional information extracted from the BodePlot. Perhaps you could export ticks, plotRange, axesOrigin to a file. You can import it and the x, y values and recreate the plot.

The idea is that in this case of importing we can't use ticks, plotrange, axes origin because we do not know the expression of the function h like when we exported (because when we exported we knew the expression of the function h). In this case, when we import, in principle the expression of the function h is not known by us, we only have that excel with the points for the x-axis and the y-axis and we know that it is a Bode diagram, and we need to plot those points, and finally to look like a Bode plot these points plotted on plot (meaning, Magnitude Plot (FirstGraphPlot) to have on y axis dB, and on x axis Log10 scale, and Phase Plot (SecondGraphPlot) to have on y axis degree, and on x axis Log10 scale).

We import because we do not know the function h from the beginning, and we want to reconstruct the Bode diagram corresponding to the function h with those points given in excel, so we can't use ticks, plotrange, axes origin, since we only have that excel with those points and nothing else. Those points from excel file they can come from a simulation for example, in which we do not have the mathematical expression of the simulation result at least at the beginning.

In this case we know the expression of the function h (because it's just an example now), but in general we don't know the expression of h when importing.

Since the other software you are working with can export to Excel, maybe you could import data exported from the other software into Mathematica rather than the other way around? That might be a better option.

Well, also in the case of other software when I export a plot, then the export will also be in the form of an excel file (or something like this) with the points for the x-axis and y-axis (as above example FirstGraphPlot.xlsx). So, even if I export the graph from another software, in Mathematica I still have to import an excel file, so I will still have some points to plot. The problem in Mathematica is how with those points and using ListLinePlot or other functions I can reconstruct the Bode diagram. The problem in Mathematica is related to the x-axis, which must be specified in the plot as a logarithmic axis. As far as I saw, when I plot in Mathematica with ListLinePlot, the x-axis is a linear axis, and I don't know how it can be changed to a logarithmic axis (Log10). In other software (eg Mathcad), I have an option where I can specify whether the x-axis/y-axis is logarithmic or linear, regardless of the x-axis point values ​​I provide (as long as those points are positive, sub-unit or super-unit (it doesn't matter this thing))

POSTED BY: Cornel B.

I don't know how to do that without the additional information extracted from the BodePlot. Perhaps you could export ticks, plotRange, axesOrigin to a file. You can import it and the x, y values and recreate the plot.

Earlier you mentioned

I will be dealing with imports and exports from several software. And I want to be able to both export and import graphs and other data from Mathematica to/from other software.

and

in other software (because the software I use lets me import/export excel files, but not Mathematica files, that's why I need to be able to import/export from Mathematica to excel and vice-versa if needed).

Since the other software you are working with can export to Excel, maybe you could import data exported from the other software into Mathematica rather than the other way around? That might be a better option.

WL supports importing a lot of different formats

$ImportFormats // Length
(* 242 *)
POSTED BY: Rohit Namjoshi
Posted 1 year ago

Rohit Namjoshi

Let's say that now I would like to import an excel file into Mathematica, and then to plot the Bode diagrams from data from that excel (let's say we know that in that excel file there are points that allow us to build Bode diagrams)). For example, let's take one of the exported files: FirstGraphPlot

test = Import["C:\\Desktop\\FirstGraphPlot.xlsx"]

enter image description here

enter image description here

Normally, the above Bode plot should look like this:

enter image description here

or (better)

enter image description here

The idea is that in the first image, the x-axis is not logarithmic... And the problem is this: with those imported points (where {x, y} - x represents the points on the x-axis, and y represents the points on the y-axis), how can I make a Bode plot similar to one of the 2 above graphs? How can I make the x-axis the logarithmic axis for the x-axis points with ListLinePlot or any other function, and having available only those imported points from excel (which represent 100% the points of the 2 Bode diagrams (which are actually one and the same graph) shown above)?

Mathematica 13.2 Notebook file attached. FirstGraphPlot excel file (.xlsx) attached.

Thank you.

POSTED BY: Cornel B.
Posted 1 year ago

Any others ideas?

POSTED BY: Cornel B.

As I mentioned in my answer, BodePlot adjusts the ticks, plot range, origin and other aspects of the plot. I am not sure why it does this rather than generating the exact values that need to be plotted. The plot can be reproduced from the points by extracting the options BodePlot uses

ticks = Ticks /. Options[result[[1, 1, 1]]]
plotRange = PlotRange[result[[1, 1, 1]]]
axesOrigin = AxesOrigin /. Options[result[[1, 1, 1]]]

ListPlot[points[[1]],
 PlotRange -> plotRange,
 Ticks -> ticks,
 AxesOrigin -> axesOrigin]

If you want to reproduce the plot in Excel you will have to replicate this.

POSTED BY: Rohit Namjoshi
Posted 1 year ago

Rohit Namjoshi Ok. Thank you. Thanks for the support of the discussion on this topic. If something else will appear, I will post it.

POSTED BY: Cornel B.
Posted 1 year ago

Hello Carl Verdon, Ctrs Disease Control & Prevention,

Your version imports only the image into Excel, but I want numerical data so that I can redo the graphics back into Excel.

enter image description here

i want something like this:

enter image description here

In this way, with these data (so not with image), I can plot graphically y=f(x). And so I can redo the graphs from Mathematica in excel too, or I can further import that excel elsewhere, in other software (because the software I use lets me import/export excel files, but not Mathematica files, that's why I need to be able to import/export from Mathematica to excel and vice-versa if needed).

Thank you.

POSTED BY: Cornel B.

As an aside, the plots can also be easily copied and pasted into Excel.

Rasterize[Result, ImageResolution -> 300] // CopyToClipboard
POSTED BY: Carl Verdon

Exporting an expression to XLSX exports the InputForm of the expression. For Plot it is a Graphics expression which Excel knows nothing about. If you want to export the points corresponding to the plots you need to extract them from the Graphics (or capture them using EvaluationMonitor). To extract them

h = TransferFunctionModel[1/s, s]
result = 
 BodePlot[h[I*2*\[Pi]*freq], {freq, 10^-3, 10^6}, PlotRange -> {{-90, 60}, {-180, 0}}]

points = Cases[result, Line[data_] :> data, Infinity]

Check that the points reproduce the plots

ListPlot /@ points

Note that the BodePlot eliminates negative x values and places tick marks on a log scale.

Export to CSV

forExport = points // Map[(Prepend[#, {"x", "y"}]) &]
MapThread[Export[#1, #2] &, {{"plot1.csv", "plot2.csv"}, forExport}]

Is there a reason for regenerating the plots in Excel?

POSTED BY: Rohit Namjoshi
Posted 1 year ago

Hello Rohit Namjoshi,

The points do not reproduce the plots:

The above code does not produce the first graph very well (neither the x-axis nor the y-axis).

enter image description here

On the second graph, the code does not reproduce the x-axis well. enter image description here

Is there a reason for regenerating the plots in Excel? - Rohit Namjoshi

Yes. I will be dealing with imports and exports from several software. And I want to be able to both export and import graphs and other data from Mathematica to/from other software.

Mathematica 13.2 Notebook file attached.

Thank you.

Attachments:
POSTED BY: Cornel B.
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract