{
  "id": 8284,
  "title": "Newbie R Question – Creating CSV from Matlab",
  "url": "/competitions/decoding-the-human-brain/discussion/8284",
  "author_name": "",
  "post_date": "2014-05-26T13:14:36.220Z",
  "votes": null,
  "comment_count": 3,
  "views": 1226,
  "content": "<p>Hi!</p>\n<p>I am learning R and trying to create a CSV from the source MatLab files. I am aware there are some better tools for doing this, but I want to understand how R handles files. There is one example online I was reviewing: <a href=\"https://raw.githubusercontent.com/FBK-NILab/DecMeg2014/master/matlab/convert2ft.m\" target=\"_blank\">https://raw.githubusercontent.com/FBK-NILab/DecMeg2014/master/matlab/convert2ft.m</a></p>\n<p>Specifically, I am stuck interpreting this loop:</p>\n<p><code>for j = 1 : size(data.X,1)</code><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </code></p>\n<p><code>&nbsp;&nbsp; data_ft.time{j} = data.tmin:1/data.sfreq:data.tmax-1/data.sfreq;</code></p>\n<p><code>&nbsp;&nbsp; data_ft.trial{j} =&nbsp; squeeze(data.X(j,:,:));</code></p>\n<p><code>end</code></p>\n<p>The actual line I am confused with is:</p>\n<p><code>data_ft.time{j} = data.tmin:1/data.sfreq:data.tmax-1/data.sfreq;</code></p>\n<p>If I am understanding the data structure correctly, the <strong>tmin</strong>, <strong>sfreq</strong>, and <strong>tmax</strong> values are static. If I am reading this line correctly, the <em>data_ft.time{j}</em> value would always be the same value. Is this true? What would the R equivalent of this line be?</p>\n<p>This is what I have so far in case someone sees something else wrong with my code: I am only working with the first file until I get this worked out and into sql server.</p>\n<p><code>data &lt;- readMat(&quot;C:\\\\Users\\\\Administrator\\\\Documents\\\\MindContest\\\\data\\\\train_subject01.mat&quot;)</code></p>\n<p><code>data.channellabels = c('MEG0113','MEG0112','MEG0111','MEG0122','MEG0123','MEG0121','MEG0132','MEG0133','MEG0131','MEG0143','MEG0142','MEG0141','MEG0213','MEG0212','MEG0211','MEG0222','MEG0223','MEG0221','MEG0232','MEG0233','MEG0231','MEG0243','MEG0242','MEG0241','MEG0313','MEG0312','MEG0311','MEG0322','MEG0323','MEG0321','MEG0333','MEG0332','MEG0331','MEG0343','MEG0342','MEG0341','MEG0413','MEG0412','MEG0411','MEG0422','MEG0423','MEG0421','MEG0432','MEG0433','MEG0431','MEG0443','MEG0442','MEG0441','MEG0513','MEG0512','MEG0511','MEG0523','MEG0522','MEG0521','MEG0532','MEG0533','MEG0531','MEG0542','MEG0543','MEG0541','MEG0613','MEG0612','MEG0611','MEG0622','MEG0623','MEG0621','MEG0633','MEG0632','MEG0631','MEG0642','MEG0643','MEG0641','MEG0713','MEG0712','MEG0711','MEG0723','MEG0722','MEG0721','MEG0733','MEG0732','MEG0731','MEG0743','MEG0742','MEG0741','MEG0813','MEG0812','MEG0811','MEG0822','MEG0823','MEG0821','MEG0913','MEG0912','MEG0911','MEG0923','MEG0922','MEG0921','MEG0932','MEG0933','MEG0931','MEG0942','MEG0943','MEG0941','MEG1013','MEG1012','MEG1011','MEG1023','MEG1022','MEG1021','MEG1032','MEG1033','MEG1031','MEG1043','MEG1042','MEG1041','MEG1112','MEG1113','MEG1111','MEG1123','MEG1122','MEG1121','MEG1133','MEG1132','MEG1131','MEG1142','MEG1143','MEG1141','MEG1213','MEG1212','MEG1211','MEG1223','MEG1222','MEG1221','MEG1232','MEG1233','MEG1231','MEG1243','MEG1242','MEG1241','MEG1312','MEG1313','MEG1311','MEG1323','MEG1322','MEG1321','MEG1333','MEG1332','MEG1331','MEG1342','MEG1343','MEG1341','MEG1412','MEG1413','MEG1411','MEG1423','MEG1422','MEG1421','MEG1433','MEG1432','MEG1431','MEG1442','MEG1443','MEG1441','MEG1512','MEG1513','MEG1511','MEG1522','MEG1523','MEG1521','MEG1533','MEG1532','MEG1531','MEG1543','MEG1542','MEG1541','MEG1613','MEG1612','MEG1611','MEG1622','MEG1623','MEG1621','MEG1632','MEG1633','MEG1631','MEG1643','MEG1642','MEG1641','MEG1713','MEG1712','MEG1711','MEG1722','MEG1723','MEG1721','MEG1732','MEG1733','MEG1731','MEG1743','MEG1742','MEG1741','MEG1813','MEG1812','MEG1811','MEG1822','MEG1823','MEG1821','MEG1832','MEG1833','MEG1831','MEG1843','MEG1842','MEG1841','MEG1912','MEG1913','MEG1911','MEG1923','MEG1922','MEG1921','MEG1932','MEG1933','MEG1931','MEG1943','MEG1942','MEG1941','MEG2013','MEG2012','MEG2011','MEG2023','MEG2022','MEG2021','MEG2032','MEG2033','MEG2031','MEG2042','MEG2043','MEG2041','MEG2113','MEG2112','MEG2111','MEG2122','MEG2123','MEG2121','MEG2133','MEG2132','MEG2131','MEG2143','MEG2142','MEG2141','MEG2212','MEG2213','MEG2211','MEG2223','MEG2222','MEG2221','MEG2233','MEG2232','MEG2231','MEG2242','MEG2243','MEG2241','MEG2312','MEG2313','MEG2311','MEG2323','MEG2322','MEG2321','MEG2332','MEG2333','MEG2331','MEG2343','MEG2342','MEG2341','MEG2412','MEG2413','MEG2411','MEG2423','MEG2422','MEG2421','MEG2433','MEG2432','MEG2431','MEG2442','MEG2443','MEG2441','MEG2512','MEG2513','MEG2511','MEG2522','MEG2523','MEG2521','MEG2533','MEG2532','MEG2531','MEG2543','MEG2542','MEG2541','MEG2612','MEG2613','MEG2611','MEG2623','MEG2622','MEG2621','MEG2633','MEG2632','MEG2631','MEG2642','MEG2643','MEG2641')</code></p>\n<p><code>options(scipen=999)</code></p>\n<p><code>fileConn &lt;- file(&quot;train_subject01.csv&quot;, open=&quot;a&quot;)</code><code>trialCount = length(data$y)</code></p>\n<p><code>for (trial in 1: trialCount){</code><code>&nbsp; </code></p>\n<p><code>&nbsp;&nbsp; for (channel in 1:306){</code><code>&nbsp;&nbsp;&nbsp; </code></p>\n<p><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (time in 1:375){</code></p>\n<p><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #data.tmin:1/data.sfreq:data.tmax-1/data.sfreq;</code><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </code></p>\n<p><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cat(trial, ',', data$y[trial], ',', channel, ',', data. channellabels[channel], ',', data$tmin, ':', 1/data$sfreq, ':', data$tmax-1/data$sfreq, ',', data$X[trial, channel, time], '\\n', file=fileConn, append=TRUE, sep=&quot;&quot;)</code><code>&nbsp;&nbsp;&nbsp; </code></p>\n<p><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</code><code>&nbsp; </code></p>\n<p><code>&nbsp;&nbsp; }</code></p>\n<p><code>}</code></p>\n<p><code>close(fileConn)</code></p>\n<p>Sorry if the code formatting is off; struggling with the editor. Thanks for you help!</p>\n<p>Mo</p>",
  "messages": [
    {
      "id": "47082",
      "postDate": "05/26/2014 13:14:36",
      "content": "<p>Hi!</p>\n<p>I am learning R and trying to create a CSV from the source MatLab files. I am aware there are some better tools for doing this, but I want to understand how R handles files. There is one example online I was reviewing: <a href=\"https://raw.githubusercontent.com/FBK-NILab/DecMeg2014/master/matlab/convert2ft.m\" target=\"_blank\">https://raw.githubusercontent.com/FBK-NILab/DecMeg2014/master/matlab/convert2ft.m</a></p>\n<p>Specifically, I am stuck interpreting this loop:</p>\n<p><code>for j = 1 : size(data.X,1)</code><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </code></p>\n<p><code>&nbsp;&nbsp; data_ft.time{j} = data.tmin:1/data.sfreq:data.tmax-1/data.sfreq;</code></p>\n<p><code>&nbsp;&nbsp; data_ft.trial{j} =&nbsp; squeeze(data.X(j,:,:));</code></p>\n<p><code>end</code></p>\n<p>The actual line I am confused with is:</p>\n<p><code>data_ft.time{j} = data.tmin:1/data.sfreq:data.tmax-1/data.sfreq;</code></p>\n<p>If I am understanding the data structure correctly, the <strong>tmin</strong>, <strong>sfreq</strong>, and <strong>tmax</strong> values are static. If I am reading this line correctly, the <em>data_ft.time{j}</em> value would always be the same value. Is this true? What would the R equivalent of this line be?</p>\n<p>This is what I have so far in case someone sees something else wrong with my code: I am only working with the first file until I get this worked out and into sql server.</p>\n<p><code>data &lt;- readMat(&quot;C:\\\\Users\\\\Administrator\\\\Documents\\\\MindContest\\\\data\\\\train_subject01.mat&quot;)</code></p>\n<p><code>data.channellabels = c('MEG0113','MEG0112','MEG0111','MEG0122','MEG0123','MEG0121','MEG0132','MEG0133','MEG0131','MEG0143','MEG0142','MEG0141','MEG0213','MEG0212','MEG0211','MEG0222','MEG0223','MEG0221','MEG0232','MEG0233','MEG0231','MEG0243','MEG0242','MEG0241','MEG0313','MEG0312','MEG0311','MEG0322','MEG0323','MEG0321','MEG0333','MEG0332','MEG0331','MEG0343','MEG0342','MEG0341','MEG0413','MEG0412','MEG0411','MEG0422','MEG0423','MEG0421','MEG0432','MEG0433','MEG0431','MEG0443','MEG0442','MEG0441','MEG0513','MEG0512','MEG0511','MEG0523','MEG0522','MEG0521','MEG0532','MEG0533','MEG0531','MEG0542','MEG0543','MEG0541','MEG0613','MEG0612','MEG0611','MEG0622','MEG0623','MEG0621','MEG0633','MEG0632','MEG0631','MEG0642','MEG0643','MEG0641','MEG0713','MEG0712','MEG0711','MEG0723','MEG0722','MEG0721','MEG0733','MEG0732','MEG0731','MEG0743','MEG0742','MEG0741','MEG0813','MEG0812','MEG0811','MEG0822','MEG0823','MEG0821','MEG0913','MEG0912','MEG0911','MEG0923','MEG0922','MEG0921','MEG0932','MEG0933','MEG0931','MEG0942','MEG0943','MEG0941','MEG1013','MEG1012','MEG1011','MEG1023','MEG1022','MEG1021','MEG1032','MEG1033','MEG1031','MEG1043','MEG1042','MEG1041','MEG1112','MEG1113','MEG1111','MEG1123','MEG1122','MEG1121','MEG1133','MEG1132','MEG1131','MEG1142','MEG1143','MEG1141','MEG1213','MEG1212','MEG1211','MEG1223','MEG1222','MEG1221','MEG1232','MEG1233','MEG1231','MEG1243','MEG1242','MEG1241','MEG1312','MEG1313','MEG1311','MEG1323','MEG1322','MEG1321','MEG1333','MEG1332','MEG1331','MEG1342','MEG1343','MEG1341','MEG1412','MEG1413','MEG1411','MEG1423','MEG1422','MEG1421','MEG1433','MEG1432','MEG1431','MEG1442','MEG1443','MEG1441','MEG1512','MEG1513','MEG1511','MEG1522','MEG1523','MEG1521','MEG1533','MEG1532','MEG1531','MEG1543','MEG1542','MEG1541','MEG1613','MEG1612','MEG1611','MEG1622','MEG1623','MEG1621','MEG1632','MEG1633','MEG1631','MEG1643','MEG1642','MEG1641','MEG1713','MEG1712','MEG1711','MEG1722','MEG1723','MEG1721','MEG1732','MEG1733','MEG1731','MEG1743','MEG1742','MEG1741','MEG1813','MEG1812','MEG1811','MEG1822','MEG1823','MEG1821','MEG1832','MEG1833','MEG1831','MEG1843','MEG1842','MEG1841','MEG1912','MEG1913','MEG1911','MEG1923','MEG1922','MEG1921','MEG1932','MEG1933','MEG1931','MEG1943','MEG1942','MEG1941','MEG2013','MEG2012','MEG2011','MEG2023','MEG2022','MEG2021','MEG2032','MEG2033','MEG2031','MEG2042','MEG2043','MEG2041','MEG2113','MEG2112','MEG2111','MEG2122','MEG2123','MEG2121','MEG2133','MEG2132','MEG2131','MEG2143','MEG2142','MEG2141','MEG2212','MEG2213','MEG2211','MEG2223','MEG2222','MEG2221','MEG2233','MEG2232','MEG2231','MEG2242','MEG2243','MEG2241','MEG2312','MEG2313','MEG2311','MEG2323','MEG2322','MEG2321','MEG2332','MEG2333','MEG2331','MEG2343','MEG2342','MEG2341','MEG2412','MEG2413','MEG2411','MEG2423','MEG2422','MEG2421','MEG2433','MEG2432','MEG2431','MEG2442','MEG2443','MEG2441','MEG2512','MEG2513','MEG2511','MEG2522','MEG2523','MEG2521','MEG2533','MEG2532','MEG2531','MEG2543','MEG2542','MEG2541','MEG2612','MEG2613','MEG2611','MEG2623','MEG2622','MEG2621','MEG2633','MEG2632','MEG2631','MEG2642','MEG2643','MEG2641')</code></p>\n<p><code>options(scipen=999)</code></p>\n<p><code>fileConn &lt;- file(&quot;train_subject01.csv&quot;, open=&quot;a&quot;)</code><code>trialCount = length(data$y)</code></p>\n<p><code>for (trial in 1: trialCount){</code><code>&nbsp; </code></p>\n<p><code>&nbsp;&nbsp; for (channel in 1:306){</code><code>&nbsp;&nbsp;&nbsp; </code></p>\n<p><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (time in 1:375){</code></p>\n<p><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #data.tmin:1/data.sfreq:data.tmax-1/data.sfreq;</code><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </code></p>\n<p><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cat(trial, ',', data$y[trial], ',', channel, ',', data. channellabels[channel], ',', data$tmin, ':', 1/data$sfreq, ':', data$tmax-1/data$sfreq, ',', data$X[trial, channel, time], '\\n', file=fileConn, append=TRUE, sep=&quot;&quot;)</code><code>&nbsp;&nbsp;&nbsp; </code></p>\n<p><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</code><code>&nbsp; </code></p>\n<p><code>&nbsp;&nbsp; }</code></p>\n<p><code>}</code></p>\n<p><code>close(fileConn)</code></p>\n<p>Sorry if the code formatting is off; struggling with the editor. Thanks for you help!</p>\n<p>Mo</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "47083",
      "postDate": "05/26/2014 13:25:49",
      "content": "<p>Looks like the first line got mangled:</p>\n<p><code>data &lt;- readMat(&quot;C:\\\\Users\\\\Administrator\\\\Documents\\\\MindContest\\\\data\\\\train_subject01.mat&quot;)</code></p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "47084",
      "postDate": "05/26/2014 13:38:12",
      "content": "<p>[quote=moflaherty;47082]</p>\n<p>The actual line I am confused with is:</p>\n<p><code>data_ft.time{j} = data.tmin:1/data.sfreq:data.tmax-1/data.sfreq;</code></p>\n<p>If I am understanding the data structure correctly, the <strong>tmin</strong>, <strong>sfreq</strong>, and <strong>tmax</strong> values are static. If I am reading this line correctly, the <em>data_ft.time{j}</em> value would always be the same value. Is this true? What would the R equivalent of this line be?</p>\n<p>[/quote]</p>\n<p>I think the goal is to get all &quot;time points&quot; in that specific time window [tmin,tmax]. For example, if tmin=0, tmax=1, and sfreq=10, then data_ft.time{j} would be 0,0.1,0.2...1. So yes, it's always the same.</p>\n<p>In R you could use seq() function. Ie. seq(from=tmin, to=tmax, length.out=sfreq) gives the same result.</p>\n<p>Edit: Oh, missed the -1/data.sfreq part. Not exactly sure why it's necessary.</p>",
      "rawMarkdown": "",
      "votes": null
    },
    {
      "id": "47088",
      "postDate": "05/26/2014 15:08:53",
      "content": "<p>Thanks for your reply!</p>\n<p>If the only purpose is to get a point in time, then I believe something like this would work:</p>\n<p><code>increment = 1/data$sfreq<br>for (time in 0:374){<br>&nbsp; cat(data$tmin + (time * increment), '\\n')<br>}</code></p>\n<p>-0.5 <br>-0.496 <br>-0.492 <br>-0.488 <br>-0.484 <br>-0.48 <br>-0.476</p>\n<p>.996</p>\n<p>etc...</p>\n<p>However, that does not take me to 1 but to .996. Close enough.</p>\n<p>But, I am still not sure I understand the source formula. Regardless, I will run with this for now.</p>\n<p>Thanks again!</p>\n<p>Mo</p>",
      "rawMarkdown": "",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 47083,
      "author_name": "moflaherty",
      "author_url": "",
      "post_date": "05/26/2014 13:25:49",
      "content": "<p>Looks like the first line got mangled:</p>\n<p><code>data &lt;- readMat(&quot;C:\\\\Users\\\\Administrator\\\\Documents\\\\MindContest\\\\data\\\\train_subject01.mat&quot;)</code></p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 47084,
      "author_name": "herrahuu",
      "author_url": "",
      "post_date": "05/26/2014 13:38:12",
      "content": "<p>[quote=moflaherty;47082]</p>\n<p>The actual line I am confused with is:</p>\n<p><code>data_ft.time{j} = data.tmin:1/data.sfreq:data.tmax-1/data.sfreq;</code></p>\n<p>If I am understanding the data structure correctly, the <strong>tmin</strong>, <strong>sfreq</strong>, and <strong>tmax</strong> values are static. If I am reading this line correctly, the <em>data_ft.time{j}</em> value would always be the same value. Is this true? What would the R equivalent of this line be?</p>\n<p>[/quote]</p>\n<p>I think the goal is to get all &quot;time points&quot; in that specific time window [tmin,tmax]. For example, if tmin=0, tmax=1, and sfreq=10, then data_ft.time{j} would be 0,0.1,0.2...1. So yes, it's always the same.</p>\n<p>In R you could use seq() function. Ie. seq(from=tmin, to=tmax, length.out=sfreq) gives the same result.</p>\n<p>Edit: Oh, missed the -1/data.sfreq part. Not exactly sure why it's necessary.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 47088,
      "author_name": "moflaherty",
      "author_url": "",
      "post_date": "05/26/2014 15:08:53",
      "content": "<p>Thanks for your reply!</p>\n<p>If the only purpose is to get a point in time, then I believe something like this would work:</p>\n<p><code>increment = 1/data$sfreq<br>for (time in 0:374){<br>&nbsp; cat(data$tmin + (time * increment), '\\n')<br>}</code></p>\n<p>-0.5 <br>-0.496 <br>-0.492 <br>-0.488 <br>-0.484 <br>-0.48 <br>-0.476</p>\n<p>.996</p>\n<p>etc...</p>\n<p>However, that does not take me to 1 but to .996. Close enough.</p>\n<p>But, I am still not sure I understand the source formula. Regardless, I will run with this for now.</p>\n<p>Thanks again!</p>\n<p>Mo</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "47082": "",
    "47083": "",
    "47084": "",
    "47088": ""
  },
  "source": "meta"
}