Import data as a cell array in a script

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

 채택된 답변

Sean de Wolski
Sean de Wolski 2015년 2월 9일
편집: Sean de Wolski 2015년 2월 9일

1 개 추천

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.

추가 답변 (2개)

Star Strider
Star Strider 2015년 2월 6일

1 개 추천

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

Zynk
Zynk 2015년 2월 9일
Thank you for your answer, but in the end I have found a different solution.
Star Strider
Star Strider 2015년 2월 9일
The ‘table’ functions are a more efficient solution, but they were introduced with R2013a, and not everyone has access to them. Unless I know the version people are using, I go with the most generic solution possible.
Zynk
Zynk 2015년 2월 9일
Yes, you are right.

댓글을 달려면 로그인하십시오.

Zynk
Zynk 2015년 2월 9일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Text Data Preparation에 대해 자세히 알아보기

질문:

2015년 2월 6일

편집:

2015년 2월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by