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 ) ;
%%%%% 获取一行字符串中的各个数字,比如:0.97681 0.10723 0.64385 0.29556 1
%%%%% 需要做两件事:数字切分,读取各个数字
nums = str2num( tline ) ; %将读取的一行tline, 转化为各个数值,这里nums是一个1*5的向量,
% 每个元素是一个double类型的数值,可以对数值直接操作。
%%%%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 ) ;
1126

被折叠的 条评论
为什么被折叠?



