필터 지우기
필터 지우기

How to open a file and store it in cell array?

조회 수: 1 (최근 30일)
Juan Rosado
Juan Rosado 2012년 12월 4일
I am creating a function that opens a text file that has {8795 x 3} of data dividied in Time, Dominant Wave Perdiod, and significant_wave_height_2.
What I want is to organize this data in a 8794 rows with three columns cell array for later use
Note: I put 8794 rows because I want to ignore the first row that contains the captions.
A sample of the first 10 rows of the data goes like this,
Time[GMT] dominant_wave_period_2[s](0m) significant_wave_height_2[m](0m)
6/4/2012 13:30 10.67 0.5
6/4/2012 14:00 8 0.56
6/4/2012 14:30 8 0.49
6/4/2012 15:00 8 0.43
6/4/2012 15:30 2 0.24
6/4/2012 16:00 2.29 0.32
6/4/2012 16:30 2.67 0.38
6/4/2012 17:00 2.91 0.46
6/4/2012 17:30 3.2 0.42
and I want to store them in the variables [Time, dominant_wave_period_2, significant_wave_height_2]
I would like to know a command of file open to generate this in the workspace as a cell array.
Could you please help?

채택된 답변

Muthu Annamalai
Muthu Annamalai 2012년 12월 4일
See documentation pages for, fopen, fgetl, fread, fscanf, fclose
>> doc fgetl
Function fgetl, fread and fscanf return the character strings; so you have to use str2double() on column 2, 3 if you want to store them as numeric types.
I'm not going to solve your problem - you have enough hints to proceed. This should be a fun exercise.

추가 답변 (1개)

Babak
Babak 2012년 12월 4일
편집: Babak 2012년 12월 4일
YourFilePath = 'C:\My Documents\Myfolder1\myfile.txt';
fid = fopen(YourFilePath ,'w');
fprintf(fid,'whatever I want to write in the text file')
fclose(fid);
  댓글 수: 1
Babak
Babak 2012년 12월 4일
편집: Babak 2012년 12월 4일
You need to create the cell array first and then start opening the text file and write to it... you can write in the txt file by fprintf() command as shown above. If you have your data already stored in [Time, dominant_wave_period_2, significant_wave_height_2], then you can put them in the txt file like this:
for j= 1:size(Time,2)
fprintf(fid,'%s %d %s \n', Time{j},dominant_wave_period_2{j},significant_wave_height_2{j});
end

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

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by