1

There are many questions on topics of text files and cell arrays but none is related to the problem that I am facing. I have a text file with numeric data like this:

1,2,3,4,5
6,7,8
9,10,11,12,13,14,15

I want this text to be put into a cell array with dimensions

[1X5]
[1X3]
[1X7]

I have tried csvread but it makes a matrix and all void elements are set to 0, which is not what I want. I'd be thankful for some help, please.

2 Answers 2

1

You can try an importdata based approach, assuming data.txt to be your input text-file.

data1 = importdata('data.txt','')

You would get -

data1 = 
    '1,2,3,4,5'
    '6,7,8'
    '9,10,11,12,13,14,15'

and then -

out = cellfun(@(x) str2num(char(strsplit(x,',')))',data1,'uni',0)

You would get your desired output -

out = 
    [1x5 double]
    [1x3 double]
    [1x7 double]

You can display their values with celldisp(out) -

out{1} =
 1     2     3     4     5
out{2} =
 6     7     8
out{3} =
 9    10    11    12    13    14    15
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for your prompt reply. I copied the command and executed, but the returned cells are all empty. I did change the file name :) but is there anything else that I'm supposed to change?
@Imran Well for the given format of the text file, it worked for me. Not sure about the empty cells issue. No, just editing the filename must have worked. Maybe try this and see what you get -importdata('data.txt','')?
I got three rows viz '1,2,3,4,5', '6,7,8', and '9,10,11,12,13,14,15'
@Imran Check out the edits as comments are added. Maybe you don't have strsplit with your version of MATLAB?
In that case it would give me an error. I typed strsplit and it said not enough arguments.
|
1

Use textread, specifying that empty cells be filled with NaN:

>> data = textread('data.txt', '', 'delimiter', ',' ,'emptyvalue', NaN)
data =
     1     2     3     4     5   NaN   NaN
     6     7     8   NaN   NaN   NaN   NaN
     9    10    11    12    13    14    15

Then convert each row excluding NaN's into a cell:

>> data = arrayfun(@(n) data(n,~isnan(data(n,:))), 1:size(data,1), 'uni', 0)
data = 
    [1x5 double]    [1x3 double]    [1x7 double]

>> celldisp(data)
data{1} =
     1     2     3     4     5
data{2} =
     6     7     8
data{3} =
     9    10    11    12    13    14    15

4 Comments

The returned data is double. How can we convert it to int? It sounds like a different question and may be required to ask separately, but I'm asking if the solution can be modified to accommodate the change. Thanks.
@Imran No need to ask separately because it can be done very simply: change second part to data = arrayfun(@(n) int16(data(n,~isnan(data(n,:)))), 1:size(data,1), 'uni', 0); or replace int16 by uint8,int32 etc, depending on which data type you want
Hmm I'm reading a 300x17800 file which does not have any 0's. After performing the first step the last four columns are somehow all 0's. Is there a way of simply removing all 0's from a matrix or a cell array?
@Imran To remove zeros, as well as NaNs (and convert to int16): arrayfun(@(n) int16(data(n,~isnan(data(n,:))&~(data(n,:)==0))), 1:size(data,1), 'uni', 0)

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.