Question

I'm having some trouble uploading a file on Matlab. I've loaded text files many times before either by using "load data" option under the file menu or with a command. The file I'm trying to read has column labels, numeric data and the first three columns consist of text data. When I attempted to upload the file, it said that file only consisted of one column and uploaded all the information in one column rather than 6 different columns. In the text file, the columns are separated by tabs and I set the delimiter as tabs.

Does anyone have any idea why it would upload the info into one column rather than in separate columns?

Thanks!

Was it helpful?

Solution

From the details you provided about the shape of your text-file I'm guessing it has the following form :

label1    info1    text1    1    1.1    3.1
label2    info2    text2    2    2.1    3.2
lebel3    info3    text3    3    3.1    3.3
...

You can load all this data using a loop and the fscanf function and simple loops.

If N is the number of lines of your text-file the following code might work for you:

f=fopen('test.txt');

for k=1:N
    for i=1:3
        a{k,i} = fscanf(f,'%s',1);
    end
b(k,:) = fscanf(f,['%f' '%f' '%f'],3);
end

fclose(f);

Then you will have one cell-array of strings a:

a = 

'label1'    'info1'    'text1'
'label2'    'info2'    'text2'
'lebel3'    'info3'    'text3'

And one array with numeric data b:

b =

1.0000    1.1000    3.1000
2.0000    2.1000    3.2000
3.0000    3.1000    3.3000

In order to get further information I advise you to read this page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top