Reputation: 5495
I see references on how to read a CSV file, such as:
X = csvread('gs_train.csv');
However, I can not find any reference when the CSV data is in a variable.
Specifically, I have:
output = 1546405200000,38.7225,39.7125,38.5575,39.48,148158948
1546491600000,35.995,36.43,35.5,35.5475,365248780
1546578000000,36.1325,37.1375,35.95,37.065,234284280
1546837200000,37.175,37.2075,36.475,36.9825,219111056
1546923600000,37.39,37.955,37.13,37.6875,164101256
1547010000000,37.8225,38.6325,37.4075,38.3275,180396324
1547096400000,38.125,38.4925,37.715,38.45,143122680
1547182800000,38.22,38.425,37.8775,38.0725,108082828
1547442000000,37.7125,37.8175,37.305,37.5,129756744
1547528400000,37.5675,38.3475,37.5125,38.2675,114841296
1547614800000,38.27,38.97,38.25,38.735,122278824
1547701200000,38.55,39.415,38.315,38.965,119284640
1547787600000,39.375,39.47,38.9952,39.205,135004092
1548133200000,39.1025,39.1825,38.155,38.325,121575880
1548219600000,38.5375,38.785,37.925,38.48,92522280
1548306000000,38.5275,38.62,37.935,38.175,101766196
1548392400000,38.87,39.5325,38.58,39.44,133635572
1548651600000,38.9475,39.0825,38.415,39.075,104768232
1548738000000,39.0625,39.5325,38.5275,38.67,166348956
1548824400000,40.8125,41.5375,40.0575,41.3125,244337120
1548910800000,41.5275,42.25,41.14,41.61,162958596
And I would like to end up with
A =
1546405200000 38.7225 39.7125 38.5575 39.48 148158948
1546491600000 35.995 36.43 35.5 35.5475 365248780
1546578000000 36.1325 37.1375 35.95 37.065 234284280
1546837200000 37.175 37.2075 36.475 36.9825 219111056
1546923600000 37.39 37.955 37.13 37.6875 164101256
1547010000000 37.8225 38.6325 37.4075 38.3275 180396324
1547096400000 38.125 38.4925 37.715 38.45 143122680
1547182800000 38.22 38.425 37.8775 38.0725 108082828
1547442000000 37.7125 37.8175 37.305 37.5 129756744
1547528400000 37.5675 38.3475 37.5125 38.2675 114841296
1547614800000 38.27 38.97 38.25 38.735 122278824
1547701200000 38.55 39.415 38.315 38.965 119284640
1547787600000 39.375 39.47 38.9952 39.205 135004092
1548133200000 39.1025 39.1825 38.155 38.325 121575880
1548219600000 38.5375 38.785 37.925 38.48 92522280
1548306000000 38.5275 38.62 37.935 38.175 101766196
1548392400000 38.87 39.5325 38.58 39.44 133635572
1548651600000 38.9475 39.0825 38.415 39.075 104768232
1548738000000 39.0625 39.5325 38.5275 38.67 166348956
1548824400000 40.8125 41.5375 40.0575 41.3125 244337120
I am not sure if Matlab and Octave have the same solution to handle this case.
Upvotes: 1
Views: 168
Reputation: 22225
In addition to rahnema1's one-liner, you can do this with textscan, but you need to provide manually how many columns you need to parse. The output of textscan is in cell form, so if your csv is strictly numeric you can convert it to a numeric matrix:
cell2mat( textscan( output, "%f,%f,%f,%f,%f,%f" ) )
However, the textscan approach may be useful if your csv file also contains non-numeric fields you want to capture, in which case you can leave the output as a cell.
Upvotes: 2
Reputation: 15837
In Octave I can use str2num
to convert a comma separated string to a matrix:
A = str2num (output);
Upvotes: 1
Reputation: 23858
Like most Matlab input/output functions, there's no option to have csvread
or read*
read it from a variable or other in-memory data source. You have to bounce it to a temporary file and read that.
Bit of a shortcoming in the Matlab standard library, if you ask me.
If you're desperate to do it in-memory, you could resort to using a Java CSV parsing library that supports reading InputStreams: grab the raw bytes of the string, wrap that in a Java ByteArrayInputStream, and parse from that. To make it work efficiently, you'd probably need to write a bit of custom Java code though, so the results could be passed back efficiently as arrays instead of requiring multiple Java method calls to get. Java method calls from M-code are slow.
Or make a RAMdisk and use that for your temp files if you're desperate for speed.
And yes, it's the same with Octave.
Upvotes: 1
Reputation: 96
I searched the Matlab documentation page for functions operating on strings and found the "replace" function. It seems like that would do what you're looking for.
In your case you do:
A = replace(X, ",", " ");
Upvotes: 0