MATLAB Answers

Import data as a cell array in a script

102 views (last 30 days)
Zynk
Zynk on 6 Feb 2015
Edited: Sean de Wolski on 9 Feb 2015
I need to import data as a cell array to Matlab for many files and later 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.
Thank you

Accepted Answer

Sean de Wolski
Sean de Wolski on 9 Feb 2015
Edited: Sean de Wolski on 9 Feb 2015
Zynk, in the import tool, after you make your selections, there's an option to "Generate function". You could then use this function on all of your files by looping over the filenames.
  1 Comment
Zynk
Zynk on 9 Feb 2015
Thank you, I hadn't noticed that.

Sign in to comment.

More Answers (2)

Star Strider
Star Strider on 6 Feb 2015
You do not give the actual file format or the file itself, so I cannot provide exact code. I would use the textscan function.
A possible (untested) way to code it would be:
fidi = fopen( filename );
data = textscan(fidi, '%f%f', 'Delimiter',',');
Be sure to explore the other name-value pair arguments if necessary.
  3 Comments

Sign in to comment.


Zynk
Zynk on 9 Feb 2015
My final objective was to get a structure-type variable, so I found this solution:
file= readtable('file.txt','Delimiter',',');
result=table2struct(file);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by