0

I need to import data as a cell array to Matlab for many files and later on perform some operations to them. Is there any way I can import the data in a script to do it automatically instead of manually?

What I do manually is:

-Home > Import Data

-Choose .txt file

-As a cell array, 2 columns as text.

-Column delimiters: comma.

UPDATE: Here a small piece of my .txt file:

/home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/d-0197.bmp, [329 210 50 51]
/home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/c-0044.bmp, [215 287 59 48]
/home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/e-0114.bmp, [298 244 46 45]
/home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/102.bmp, [243 126 163 143]
/home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/e-0120.bmp, [250 400 48 48]

Result: cell array with 2 text columns.

-The first column contains the paths to pictures:

'/home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/d-0197.bmp'
'/home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/c-0044.bmp'
'/home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/e-0114.bmp'
'/home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/102.bmp'
'/home/camroom/Dropbox/Internship/Matlab/Cascade1/training/positive/rawdata/e-0120.bmp'

-The second column contains the arrays as text:

' [329 210 50 51]'
' [215 287 59 48]'
' [298 244 46 45]'
' [243 126 163 143]'
' [250 400 48 48]'

Thank you

3
  • 1
    Sure there is. Post a small example with file contents and desired result of the import Commented Feb 6, 2015 at 13:02
  • Thanks, I just posted it. Let me know if you need more information. Commented Feb 6, 2015 at 13:41
  • I think this is enough to understand what you want. I can't take a look at it now. Maybe someone will. Also, I'll try to come back later Commented Feb 6, 2015 at 14:08

2 Answers 2

1

You can use importdata. This gives each line in a different cell. You then split each line using regexp with the 'split' option:

y = importdata('filename.txt');
y = regexp(y, ',', 'split'); %// or ', '
y = cat(1, y{:});
Sign up to request clarification or add additional context in comments.

Comments

1

Try this-

T = readtable('fileName.txt','Delimiter',',','ReadVariableNames',false)

2 Comments

This generates a table, not a cell array. You would have to convert from table to cell array
Indeed I was looking for a cell array, but actually what I want as my final result is a structure, so I have to convert it anyway. If there is a way to obtain it as a structure directly, please share it. If not, this will do. Thank you

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.