0

I'm writing a code to import data from a CSV file and convert it into an Octave matrix. The imported data can be seen in the following snap:

DataImportedFromCSV

In the next step I added the following command to delete the commas and "":

meas_raw_temp = strrep(meas_raw_temp,',',' ')

And then I get the data format in the following form:

DataConvertedWithoutComma

The problem is that Octave still sees the data as 1 single 1-dimensional array. i.e., when I use the size command I get a single number, i.e. 2647. What I need to have is a matrix output, with each line of the snaps being a row of the matrix, and with each element separated.

Any thoughts?

3
  • 1
    Please see: Why is “Can someone help me?” not an actual question? Commented Oct 4, 2018 at 12:07
  • 1
    Can you post a simple example that doesn't include screenshots of the data. Also how are you reading it in? as that's probally where the issue is ? Commented Oct 4, 2018 at 12:18
  • 1
    Or if the object is read in CSV data rather than having to create a function to do it, pkg load io, then use the csvread function Commented Oct 4, 2018 at 15:46

1 Answer 1

1

Here's what's happening.

  1. You have a 1-dimensional (rows only) cell array. Each element (i.e. cell) in the cell array contains a single string.

  2. Your strings contain commas and literal double-quotes in them. You have managed to get rid of them all by replacing them in-place with an 'empty string'. Good. However that doesn't change the fact that you still have a single string per cell.

  3. You need to create a for loop to process each cell separately. For each cell, split the string into its components (i.e. 'tokens') using ' ' (i.e. space) as the delimiter. You can use either strsplit or strtok appropriately to achieve this. Note that the 'tokens' you get out of this process are still of 'string' type. If you want numbers, you'll need to convert them (e.g. using str2double or something equivalent).

  4. For each cell you process, find a way to fill the corresponding row of a preallocated matrix.

  5. As Adriaan has pointed out in the comments, the exact manner in which you follow the steps above programmatically can vary, therefore I'm not going to provide the range of possible ways that you could do so, and I'm limiting the answer to this question to the steps above, which is how you should think about solving your problem.

If you attempt these steps and get stuck on a 'programmatic' aspect of your implementation, feel free to ask another stackoverflow question.

Sign up to request clarification or add additional context in comments.

1 Comment

Tasos Papastylianou, many thanks for the input! I added the loop, and now the code works just perfect! Many thanks again :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.