1
$\begingroup$

I have a couple of *.csv files containing data from several experiments. From those, two columns shall be extracted each, to form a new list. This process shall be automated to collect all the data from all experiments of one series. For this I try to use an indexed variable a[i]. With Transpose and Array the desired list is created. But when I try to export my test case to a *.csv it contains those curly brackets and quotation marks (the first two lines contain strings). Within this forum a number of people suggested to use Flatten to get rid of the unwanted characters. This works but completely destroys the structure of the table, which I want to keep.

When I use Join on my testcase there are no brackets or quotation marks in the *.csv. But as far as I understand the indexed variables are better for my task since the number of experiments within the different series will change. This assumption is based on answers of the following thread: https://stackoverflow.com/questions/11783760/how-do-i-save-multiple-variables-with-an-iterated-name-in-mathematica

I suppose it might be "just" an issue with the data format. Because it looks like Mathematica is exporting the list instead of the values within the list itself. Unfortunately I can't figure out how to fix it.

Thank you in advance!

--Edit 1--

Here is a code example of my problem. I hope this helps better understanding the problem.

I'm importing the *.csv files to the indexed varibale, using colums 1 and 4.

heatcombarrayex[1] =
    Part[
        Import[
            TemplateApply[NotebookDirectory[] <> StringTemplate[
                newfilename <> "_``"][ToString[1] <> "-" <> ToString[1]]] <> 
                "\\" <> TemplateApply[StringTemplate[
                newfilename <> "``"][ToString[1] <> "-" <> ToString[1]]] <> 
                "_devc.csv"], 
    All, {1, 4}];

Then I use Array to show all the entries of the indexed variable. For my testcase I use two entries.

heatcombarrayoutex = Transpose[Array[heatcombarrayex, 2]];

Then the data is exported to a *.csv again.

Export[TemplateApply[
  NotebookDirectory[] <> 
   "heatcombarrayex.csv"], heatcombarrayoutex, "CSV"]

This shows the first four lines of the result (Also I realised that I forget to mention that the second column is also wrapped in qoutation marks.):

{s", "kW/kg"}","{"s", "kW/kg"}" {Time", "hrrpum"}","{"Time", "hrrpum"}" {0., 0.},"{0., 0.}" {4.21, 0.033500582},"{4.21, 0.041624561}"

And this is how it should look like:

s,kW/kg,s,kW/kg Time,hrrpum,Time,hrrpum 0.,0.,0.,0. 4.21,0.033500582,4.21,0.041624561

So the question is: How to get rid of those extra characters?

-- Edit 2 --

For the the test case I use two data files (which can be found via the added link). But I only posted the code to import file number one, since the code for the following is basically the same, just the number changes.

In Mathematica the heatcombarrayoutex looks like this:
{{{"s", "kW/kg"}, {"s", "kW/kg"}}, {{"Time", "hrrpum"}, {"Time", "hrrpum"}}, {{0., 0.}, {0., 0.}}, {{4.21, 0.0335006}, {4.21, 0.0416246}}, ...995..., {{4200., 0.}, {4200., 0.}}}

I have two example files included for importing, which can be found following this link

$\endgroup$
  • 4
    $\begingroup$ Do you have any code and data which illustrates your problem ? $\endgroup$ – image_doctor Aug 12 '15 at 16:10
  • $\begingroup$ Yes, I have some code added. I apologise that I forgot to put it there when I posted my issue. $\endgroup$ – Gaston Aug 13 '15 at 7:46
  • $\begingroup$ In your result you have {s", "kW/kg"}" and {Time", "hrrpum"}", these seem to have unbalanced quotes, is that intentional? A full example with input, code and in error output, plus desired output, would help. $\endgroup$ – image_doctor Aug 13 '15 at 8:30
  • $\begingroup$ No, this is not intentional, Mathematica somehow "made this up". This is why I believe there might be an issue with the data format. I suppose I need to tell Array something about the data format otherwise it might use some default which leads to this issue. The input file structure is like the "how it should look like"-example. Also Join produces the output in the right way. $\endgroup$ – Gaston Aug 13 '15 at 9:04
  • $\begingroup$ To work out what went wrong it will be easier to have a full path from input data , through code, to output $\endgroup$ – image_doctor Aug 13 '15 at 9:06
2
$\begingroup$

Here is an answer using a small amount of the two example data files you supplied.

Read the first 4 lines of each file:

 h = Import[#][[1 ;; 4, {1, 4}]] & /@ FileNames["/SampleData/Heat*"];

Transpose, flatten and export the data:

Export["/SampleData/heatcombarrayex.csv", Flatten/@Transpose@h]

The issue you were having is that when you transpose your data, you have nested lists which are represented in CSV format in bracketed form.

{{{"s", "kW/kg"}, {"s", "kW/kg"}}, {{"Time", "hrrpum"}, {"Time", "hrrpum"}}, {{0., 0.}, {0., 0.}}, {{4.21, 0.0335006}, {4.21, 0.0416246}}}

You need to remove that nesting by using Flatten.

{{"s", "kW/kg", "s", "kW/kg"}, {"Time", "hrrpum", "Time", "hrrpum"}, {0., 0., 0., 0.}, {4.21, 0.0335006, 4.21, 0.0416246}}

And the contents of the exported file:

s,kW/kg,s,kW/kg Time,hrrpum,Time,hrrpum 0.,0.,0.,0. 4.21,0.033500582,4.21,0.041624561

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.