Friday, March 30, 2012

Automatically M-file Generator (GUI MATLAB)

วันนี้ผมจะนำเสนอ GUI สำหรับสร้าง m-file โดยอัตโนมัติ เพื่อประโยชน์สำหรับติดต่อกับผู้ใช้ โดยที่ผู้ใช้ต้องใส่พารามิเตอร์ต่างๆ ตัวอย่างนี้เป็นการสร้างฟังก์ชันไฟล์ หรือจะเรียกว่า library file เพื่อเรียกมาใช้ในครั้งต่อไปสำหรับโปรแกรมหนึ่งๆ ที่จำเป็นต้องใช้ ข้อมูล สมการ เดิมๆ บ่อยๆ
%------------------------------------------------------------------------
ยกตัวอย่าง function ใน MATLAB อธิบายสั้นๆ ดังนี้
function [ a , b ] = plus( x , y )

a=y+x-1;
b=y+x+1;

เมื่อต้องการบันทึกไฟล์นี้ MATLAB จะบังคับให้บันทึกเป็นชื่อ plus ครับ
การทำงานของไฟล์นี้ ผู้ใช้ต้องมีค่า x และ y ให้ฟังก์ชันนี้ เมื่อทำงานเสร็จ จะส่งค่า a และ b ออกมา
%------------------------------------------------------------------------

ผมขอยกตัวอย่างโปรแกรมขึ้นมาแสดงก่อนนะครับ รูปด้านล่างเป็นหน้าตา layout และโปรแกรมที่พร้อมทำงานครับ ไฟล์ที่สร้างขึ้นมีชื่อว่า LibraryGen.m และ LibraryGen.fig


และรูปด้านล่างเป็นหน้าต่างโปรแกรมที่ทำงานแล้วครับ


ผมขอรวบรัดนิดนึงครับ สำหรับงานนี้ ส่วนหลักๆที่เราต้องเพิ่มเติม source code มีดังนี้ครับ
- การรับค่าจาก edit text ใช้ get(handles. tag ของ edittext ที่เราตั้ง, 'string')
แล้วเก็บเป็น handles.ตัวแปรที่ใช้เก็บค่า สังเกตจากตัวอย่างด้านล่างครับ (บรรทัดสีแดง)
- การเขียนไฟล์ไปบน editor พร้อมบันทึกค่า ในที่นี้เป็น function file ครับ
คำสั่งที่ต้องใช้ครับ fopen and fprintf

function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
effectiveSignal=get(handles.edit1,'string');
handles.EffectiveSignal=effectiveSignal;
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
guidata(gcbo,handles);

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
slope=get(handles.edit2,'string');
handles.Slope=slope;
% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
guidata(gcbo,handles);

% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
yintercept=get(handles.edit3, 'string');
handles.Yintercept=yintercept;
% Hints: get(hObject,'String') returns contents of edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
guidata(gcbo,handles);

% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function edit4_Callback(hObject, eventdata, handles)
% hObject handle to edit4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
rsquare=get(handles.edit4, 'string');
handles.Rsquare=rsquare;
% Hints: get(hObject,'String') returns contents of edit4 as text
% str2double(get(hObject,'String')) returns contents of edit4 as a double
guidata(gcbo,handles);

% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function stdError_Callback(hObject, eventdata, handles)
% hObject handle to stdError (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
standarderror=get(handles.stdError, 'string');
handles.StandarderroR=standarderror;
% Hints: get(hObject,'String') returns contents of stdError as text
% str2double(get(hObject,'String')) returns contents of stdError as a double
guidata(gcbo,handles);

% --- Executes during object creation, after setting all properties.
function stdError_CreateFcn(hObject, eventdata, handles)
% hObject handle to stdError (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

function LiBfilename_Callback(hObject, eventdata, handles)
% hObject handle to LiBfilename (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.libname=get(handles.LiBfilename,'string');
% Hints: get(hObject,'String') returns contents of LiBfilename as text
% str2double(get(hObject,'String')) returns contents of LiBfilename as a double
guidata(gcbo,handles);

% --- Executes during object creation, after setting all properties.
function LiBfilename_CreateFcn(hObject, eventdata, handles)
% hObject handle to LiBfilename (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

% --- Executes on button press in save.
function save_Callback(hObject, eventdata, handles)
% hObject handle to save (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
button = questdlg('Do you want to save new library?', ...
'Exit Dialog','Yes','No','No');

switch button
case 'Yes',
disp('Exit RGB SpecAnal');
%Save variables to matlab.mat


Effective=handles.EffectiveSignal;
Yintercept=handles.Yintercept;
[xbel xlabel]=size(Yintercept);
Slope=handles.Slope;
Rsquare=handles.Rsquare;
Standard_Error=handles.StandarderroR;
%-------------------------------------------------------------
functionMfilename=handles.libname;
functionname=functionMfilename;
filename = fullfile([functionMfilename,'.m']);
fid = fopen(filename,'wt');
%----- function [ccUnknown]=library(UnknownIntensity)---------
fprintf(fid,'function [ccUnknown EffectiveSignal]=');
fprintf(fid,functionname);
fprintf(fid,'(UnknownIntensity)\n');
fprintf(fid,'%%this function is used to calculate the unknown parameter ----\n');
fprintf(fid,'%%created on automatically M-file generator\n');
fprintf(fid,'%%-----------------------------------------------------------------\n');
%-------------------------------
fprintf(fid,'[mrows ncols]=size(concentration);\n');
fprintf(fid,'ccUnknown=ones(mrows,ncols);\n');
fprintf(fid,'%%----alculation of Unknown concentration----\n');

if Yintercept(1)==45
fprintf(fid,'ccUnknown=(UnknownIntensity+');
fprintf(fid,Yintercept(2:xlabel));
else
fprintf(fid,'ccUnknown=(UnknownIntensity-');
fprintf(fid,Yintercept);
end
fprintf(fid,')./');
fprintf(fid,'(');
fprintf(fid,Slope);
fprintf(fid,')');
fprintf(fid,';\n');
fprintf(fid,'%%---------------------------\n');
fprintf(fid,'EffectiveSignal = ');
fprintf(fid,Effective);
fprintf(fid,';%%1 = Red, 2 = Green, 3 = Blue \n');
fprintf(fid,'R-square = ');
fprintf(fid,Rsquare);
fprintf(fid,';%%----R square from linear regression \n\n');
fprintf(fid,'Standard-error = ');
fprintf(fid,Standard_Error);
fprintf(fid,';%% \n\n');
fprintf(fid,'return ccUnknown,EffectiveSignal');

%save
%close(ancestor(hObject,'figure'))
case 'No',
%quit cancel;

end

No comments:

Post a Comment