MATLAB 09

신예진·2022년 2월 8일
0

Matlab Simulink

목록 보기
17/17

Debugger is accessible from Editor (EDITOR > Breakpoints)

Breakpoint

1) After running

① stops at breakpoint (at the beginning of the line)
② examine or modify any variable at K>>
or use datatip (position the cursor on variable name)

command+⇧+O : run step by step (step)
command+⇧+I : same as stemp but enters inside functions (step in)
command+⇧+U : (step out)
command+option+R : run to the next breakpoint (continue)

2) Quit

EDITOR > Quit debugging
command window > fn+F5

3) Conditional Breakpoint (yellow dot)

조건문 안에 마우스를 클릭한 후 > EDITOR > Breakpoints > 조건 설정
: allows to give a condition to stop for the first time

File input & output(I/O)

3 steps for general file I/O (부제 : 파일은 공책과 비슷하다)

open a data file → fopen
read or write data → fscanf, fprintf
close the file → fclose

1) fopen : open a file

fid = fopen('datafile.txt','r') % 파일명을 계속 반복해서 부르기 귀찮으니 매트랩은 파일명에 고유 번호를 붙여준다
fid: file ID (-1 when there is an error)

2) permission

r : read (file must exist)
w : write (create or overwrite)
wt : same as w but in text form (use wt not w)
a : append (create or append)
r+ : read/write (file must exist)
w+ : read/write (create or overwrite)
a+ : read/write (create or append)

3) fclose : close a file

fclose(fid)

4) fprintf : write to a file

fprintf(fid,'string containing format',variable,...)
without fid, output is written to the monitor

* format (can specify # of digits after decimal point)
%e : floating point
%f : fixed point
%d : integer
%s : string
%07.2f, %03d   - zero padding in front

* Ex
x = 0 : 0.05 : 0.2;
y = [ x', exp(x')];
fid = fopen('exp.txt','w');
fprintf( fid, 'Exponential function\n\n' );
fprintf( fid, '%7.2f %12.5f\n, y);
	** 그른데 여기서 잠깐! 저장된 file을 보면 y값이 엉망진창인 것을 알 수 있다.
	** 이유 : Conflict b/w column major and row major
	** data in memory - column major, data in a file - row major
    ** 해결 방법 : correct y → y'
close(fid);

5) fscanf

read from a file
a = fscanf( fid, 'format', size)

* file position indicator (다음 읽을 위치 추적)
  : Matlab keeps track of "where to read next" for fscanf
  
* format
no need to specify data width when reading data separated by spaces
%c : characters
%d : integers
%f, %e : real numbers

* Ex
fid = fopen( 'exp.txt', 'r' );
t = fscanf( fid, '%c', 20 );
A = fscanf( fid, '%f', [5,2] );  % correct this : 애초에 전치된 행렬로 저장 후 뒤집어라 여기선 [2,5] 한 후 A = A'
fclose( fid );
A

6) fscanf without size

a = fscanf( fid, '%c' )
  ⟹ reads all remaining part as string

a = fscanf( fid, '%e' )
  ⟹ reads only numbers in remaining part  % 문자를 마주치기 전 숫자 부분만 읽는다. 설령 문자 뒤에 숫자가 있더라도.

0개의 댓글