How to open a file and store it in cell array?
3 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Muthu Annamalai
on 4 Dec 2012
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.
0 Comments
More Answers (1)
Babak
on 4 Dec 2012
Edited: Babak
on 4 Dec 2012
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 Comment
Babak
on 4 Dec 2012
Edited: Babak
on 4 Dec 2012
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
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!