[Matlab] practice_4

dewdew·2023년 7월 2일
0

Matlab & Simulink

목록 보기
5/6
post-thumbnail

% File I/O

% 3 steps for general file I/O
% fopen -> fscanf or fprintf -> fclose

% 1. fopen
% dir *.txt
% fid = fopen('test.txt', 'r') % 고유 number 할당
% fid = fopen('output.txt', 'r') % 없는 파일은 -1 할당
% fid = fopen('output.txt', 'w') % create file
% type test.txt
% fid = fopen('test.txt', 'w') % append
% type test.txt
% fclose(17)

% 2. fprintf(write)
% x = 0 : 0.05 : 0.2;
% y = [x', exp(x')]; % column major
% fid = fopen('exp.txt', 'w');
%
% fprintf(fid, 'Exponential function\n\n');
% fprintf(fid, '%7.2f %12.5f\n', y')
% % row major, column major 불일치 해결 방법 : transposed!
% fclose(fid)
% type exp1.txt

% x = 0 : 0.05 : 0.2;
% y = [x', exp(x')];
% fid = fopen('exp.txt', 'w');
%
% fprintf(fid, 'Exponential function :\n');
% fprintf(fid, '%7.2f %12.5f\n', y');
% fclose(fid)

% 3. fscanf(read)
% fid = fopen('exp1.txt', 'r')
% % file position indicator
% t = fscanf(fid, '%c', 20)
% A = fscanf(fid, '%e', [2,5])
% A = A'
% fclose(fid)
% % transposed 사용해주자
% type exp1.txt
% A
% ++) batch file 만들 때 유용한 기능 (c : reads all / e : reads only number)

% 4. fgetl
% fid = fopen('output.txt', 'r')
% s = fgetl(fid) % file position indication
% A = fscanf(fid, '%e') % row major
%
% while(1)
% s = fgetl(fid)
% if isequal(s, '')
% break
% end
% end
% A = fscanf(fid, '%e')

% 5. eval
who
s = 'who'
eval(s)
% data open with zero padding batch file
s = ['fid = fopen(''data', num2str(1), '.txt'', ''r'')']
eval(s)

for i = 1:10
s = ['fid = fopen(''data', num2str(i, '%02d'), '.txt'', ''r'')']
eval(s)
end

% 6. batch job with text data
% ff = fopen('exp.txt', 'r')
% s = fgetl(ff)
% a = fscanf(ff, '%e') % row major from file (5x2)
% whos a % 10x1 => 5x2 : transpose 사용하기
% A = zeros(2,5)
% A(:) = a % column major 로 채워넣기
% A = A'

% filename = input('Type ascii file name (ex. output1.txt, output2.txt), 's')
% fid = fopen(filename, 'r')
% nd = 5; % number of rows
% s = fgetl(fid);
% a = fscanf(fid, '%e');
% md = length(x) / nd

% 7. exchange data with excel
load map_engine
whos
rpm
throttle
torque
% lookup table
% T = [ nan, rpm ; throttle', torque ]
% xlswrite('myengine', {'throttle & rpm'}, 'engine', 'A1') % cell array
% xlswrite('myengine', rpm, 'engine', 'B1')
% xlswrite('myengine', throttle', 'engine', 'A2')
% xlswrite('myengine', torque, 'engine', 'B2')
% rpm1 = xlsread('myengine.xls', 'engine', 'B1:J1')

profile
연습장

0개의 댓글