您的位置:首页 > 编程语言 > MATLAB

matlab txt 文件操作

2014-04-19 08:43 337 查看
fid = fopen('result.txt', 'a') ;

fprintf( fid, '%-d\n', variable) ;

fclose( fid ) ;

%%%% matlab 打开文件后,逐行读取文件

fid = fopen('result.txt', 'r') ;

id = 1 ;

while 1

tline=fgetl(fid); % read the context of a line

if ~ischar(tline),break;end

tmp = [] ;

for i =1:11 % the first 11 is the lable of the data, 将前11个字符做成字符串

tmp = [ tmp tline(i) ] ;

end

%%%% tmp is a string, can be transformed into numbers.

id_list{ id } = tmp ;

id = id + 1 ;

end

fclose( fid ) ;

%%%%matlab整个读取.txt文件,比如:整幅图像

A = load('G:\zz_myfile.txt') ; % 直接load文件, 或使用fscanf读

%%%% matlab 逐行书写到文件

fid = fopen('result.txt', 'a') ;

for i = 1:length( id_list )

tmp = [] ;

tmp = ‘hello,world!’ ;

fprintf( fid, '%s\n', tmp ) ; % write line by line

end

fclose( fid ) ;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: