File 을 Save 하는 방법과 기타 명령어들
Reference : http://knauer.tistory.com/77
주로 사용하는 언어는 C++이지만 MATLAB에서의 몇 가지 함수들이 필요할 때, 파일 입출력을 이용하여 처리하는 경우가 많다.
읽어오는 경우야 대부분 load를 사용한다던가, 드래그&드롭같은 단순한 방식을 사용하고, 그래도 되도록 파일 포맷을 만들지만, 저장은 일일이 fopen / fprintf를 사용해서 저장 했는데, 검색 해 보니 간단하게 저장 할 수 있더라...
기본적으로 MATLAB에서는 .MAT이라는 확장자의 파일 포맷을 지원하며, 이것은 binary형태로 해당 변수를 저장한다.
검색해서 나온 몇가지 명령어들..
========================================================================
보통 위에 있는 것들은 참고 용이고 실제로 사용할때는 아래와 같이 내가 깔끔하게 정리함
========================================================================
<파일을 mat으로 저장>
save('Region Histogram Model.mat')
=========================================================================
<저장한 mat 파일을 하나만 불러>
load('Region Histogram Model.mat')
==================================================================
< Plot에 나오는 그래프를 저장할 때 >
ref:http://stackoverflow.com/questions/12160184/how-to-save-a-figure-in-matlab-in-from-the-command-line
h=figure;
plot(x,y,'-bs','Linewidth',1.4,'Markersize',10);
% ...
saveas(h,name,'fig')
saveas(h,name,'jpg')
==================================================================
< Command window에 나오는 내용 몽땅 저장 >
찾아 보니까 맘에 들게 깔끔하게 나온게 하나도 없고 나온건 뭔소린지 몰라서 직접 해보고 작성
코드 시작 부분에 저장 할 이름과 확장자 선언한 뒤 끝내고 싶은 부분에 off 넣어주면됨. 뭔소리냐면
% 주석 파일 설명
diary myText.txt %시작하고 싶은 부분에 넣어서 on 시켜준다. 여기 예제에서 이름은 myText
.....코드코드......
......
diary off % 끝내고 싶은 부분( 보통 제일 마지막)에 이렇게 넣어주면 끝!
==================================================================
< How to save image results into a specific folder >
저장 방법에 뻘 소리가 많아서 뒤지다가 생각해낸 방법!!
아이디어 : 저장해야 되는 Specific folder를 Current Directory로 Change한 후에 저장을 하고 다시 원래의 Directory로 Back 해주면 된다.
즉.
1. Change the directory you want to saving folder
2. Save file using imwrite original method.
3. Go back the directory to compute other procedure....
Example)
F_Name = sprintf('%s',ImgList{i});
mkdir('C:\Users\ChoiHyungUk\Desktop\[extFeature]SemanticSeg(GeneralDB)\MRSC_Results',F_Name);
cd(strcat('C:\Users\ChoiHyungUk\Desktop\...[extFeature]SemanticSeg(GeneralDB)\MRSC_Results\',sprintf('%s',ImgList{i})));
for i=1:n
....
imwrite(final_RGB./256, sprintf('SepResults_%d__%s', iii, ImgList{i}),'bmp');
end
cd('..'); % changes the current working directory to the directory above it.
이렇게 Current Directory를 Change 한 뒤에 작업하고 다시 Go back 해주면 된다!!
==================================================================
< matrix를 dat나 txt로 저장하는 방법>
dlmwrite(sprintf('info_%d.dat',CamNumber), M, '\t');
위의 명령어를 사용하면 된다. 여기서 M은 matrix (정보가 들어있는)
==================================================================
< How to save Text file>
x = 0:.1:1; A = [x; exp(x)]; fileID = fopen('exp.txt','w'); fprintf(fileID,'%6s %12s\n','x','exp(x)'); fprintf(fileID,'%6.2f %12.8f\n',A); fclose(fileID);