Good way to import data file

6 ビュー (過去 30 日間)
Kieran Hazell
Kieran Hazell 2015 年 5 月 5 日
編集済み: Kieran Hazell 2015 年 5 月 6 日
Hey, I need to import a large data file into matlab, but I'm unsure of the best way to do it. I've been experimenting with the fopen and textscan but can't get anything to work correctly. Any help is appreciated.

採用された回答

Michael Haderlein
Michael Haderlein 2015 年 5 月 5 日
編集済み: Michael Haderlein 2015 年 5 月 5 日
If the format doesn't change through the file (such that after 10 s, suddenly also data about whatever appears), you can simply use:
fid=fopen('test.txt');
data=textscan(fid,'%*s%f','delimiter',':');
fclose(fid);
t=data{1}(1:4:end);
gs=data{1}(2:4:end);
gflat=data{1}(4:4:end);
gflong=data{1}(3:4:end);

その他の回答 (1 件)

David Sanchez
David Sanchez 2015 年 5 月 5 日
I'm sure this might be shorter, but I guess it'll be a valid solution for you:
fid = fopen('your_file_name_here.txt');
my_file = textscan(fid, '%s', 'delimiter', '\n');
my_file = my_file{1};
fclose(fid)
% now you have a cell array (my_file) with each line of your *.txt in a different
% cell
% you can extract the values like this:
L = length(my_file); % number of lines in your file
my_data = zeros(L,1); % initialization of matrix to hold your data
for k = 1:L % loop along each line
str_value = textscan(my_file{k}, '%s %s', 'delimiter', ':' );
my_data(k) = str2double(cell2mat(str_value{1,2}));
end
I guess you can figure out how to assign every four values to each variable

カテゴリ

Help Center および File ExchangeLow-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