关于数据文档的读取和存储
最近刚接触GUI界面编程,想问点菜鸟级的问题,希望高手指教.我想用GUI截面实现如下的功能: 现在有很多个dat文件,每个文件中都有6列,而且列名是相同的,只是每列中的数据不一样.现在需要从几个不同的dat文件中分别选取几列(可能是2列,也可能是3列),然后把这些列读入一个新的dat文件中.我现在已经建了一个界面,用6个Checkbox代表6个列名,用户可以进行选择.然后界面上还有一个Edit Text框,可以输入dat文件的路径.也即我想先选择路径,即选择文件,然后用Checkbox实现在这个文件中选择需要读入的列名,最户还用了一个toggle button,即一点这个toggle button就表示把以上选择的内容读入了新文件.
但进入了m文件我就一头雾水了.我想问的是对6个控件函数Checkbox我应该如何编写,即如果这个Checkbox被选中,我就用get函数把这列的内容给一个新的变量,这样可以吗?问什么老报错,说变量没定义之类的.应该怎么写?
还有我在toggle button控件函数中用了fprintf把所选列的内容读入新文件,为什么也报错?还请高手指教.
以前在这个论坛得到了很多高手的帮助和指教,先表示感谢了.
希望大家帮帮我. 原帖由 wrx 于 2007-5-23 04:35 发表 http://www.chinavib.com/forum/images/common/back.gif
最近刚接触GUI界面编程,想问点菜鸟级的问题,希望高手指教.
我想用GUI截面实现如下的功能: 现在有很多个dat文件,每个文件中都有6列,而且列名是相同的,只是每列中的数据不一样.现在需要从几个不同的dat文件中分 ...
请给出具体代码 具体代码如下:
function varargout = untitled (varargin )
gui_Singleton = 1;
gui_State = struct (‘gui_Name’ ,
mfilename , …,
‘gui_ Singleton ’,
gui_ Singleton, …
‘gui_ OpeningFcn’, @untitled_ OpeningFcn, …
‘gui_ OutputFcn’,
@untitled_ OutputFcn, …
‘gui_ LayoutFcn’,
[ ], …
‘gui_ Callback’,
[ ] ) ;
if nargin && ischar (varargin{1})
gui_State. gui_ Callback = str2func (varargin{1});
end
if nargout
= gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function untitled_ OpeningFcn (hObject, eventdata, handles, varargin)
= uigetfile (‘*.amp’ , ‘sample Dialog Box’);
handles.output = hObject;
guidata( hObject,handles);
function varargout = untitled_ OutputFcn ( hObject, eventdata, handles )
varargout {1} =handles.output;
function A_Callback
( hObject, eventdata, handles )
A_state=get(hObject, ‘ Value ’);
if A_state = = get(hObject, ‘ Max ’)
A = str2double (get(handles.A, ‘String’));
end
function B_Callback
( hObject, eventdata, handles )
B_state=get(hObject, ‘ Value ’);
if B_state = = get(hObject, ‘ Max ’)
B = str2double (get(handles.B, ‘String’));
end
function C_Callback
( hObject, eventdata, handles )
C_state=get(hObject, ‘ Value ’);
if C_state = = get(hObject, ‘ Max ’)
C = str2double (get(handles.C, ‘String’));
end
function D_Callback ( hObject, eventdata, handles )
D_state=get(hObject, ‘ Value ’);
if D_state = = get(hObject, ‘ Max ’)
D = str2double (get(handles.D, ‘String’));
end
function E_Callback ( hObject, eventdata, handles )
E_state=get(hObject, ‘ Value ’);
if E_state = = get(hObject, ‘ Max ’)
E = str2double (get(handles.E, ‘String’));
end
function F_Callback ( hObject, eventdata, handles )
F_state=get(hObject, ‘ Value ’);
if F_state = = get(hObject, ‘ Max ’)
F = str2double (get(handles.F, ‘String’));
end
function edit_Callback ( hObject, eventdata, handles )
function edit_CreateFcn ( hObject, eventdata, handles )
if ispc && isequal (get(hObject, ‘ BackgroundColor ’), get
(0, ‘ defaultUicontrolBackgroundColor ’))
set (hObject, ‘ BackgroundColor ’, ‘white’ );
end
function read_Callback ( hObject, eventdata, handles )
read_state=get(hObject, ‘ Value ’);
if read_state = = get(hObject, ‘ Max ’)
fidaus=fopen(’18.amp’,’w’)
fprintf(fidaus,’%f
%f
%f
%f
%f
%f\n’.A, B, C, D, E, F)
end
我现在用了uigetfile来引入文件,这样会弹出一个对话框,但我的本意是想用edittext来输入文件路径的,而不想弹出对话框,具体界面在附件里.但我不知道
function edit_Callback ( hObject, eventdata, handles )
function edit_CreateFcn ( hObject, eventdata, handles )
这2个函数应该怎么编,即我怎样通过edittext中的文件路径来读入文件的内容,还请高手指教.
还有我现在的程序运行以后会出现以下的错误:
fidaus=
1
??? Undefined function or variable 'A'.
Error in ==>read_Callback
fprintf(fidaus,’%f
%f
%f
%f
%f
%f\n’.A, B, C, D, E, F)
Error in ==>gui_mainfcn
feval(varargin{:});
Error in ==>
gui_mainfcn(gui_State,varargin{:});
??? Error while evaluating uicontrol Callback
实在是不知道该怎么办了,还请高手帮忙,另外界面在附件里.
万分感谢!
不好意思代码有点串行,我再发一遍
function varargout = untitled (varargin )
gui_Singleton = 1;
gui_State = struct (‘gui_Name’ , mfilename , …,
‘gui_ Singleton ’,gui_ Singleton, …
‘gui_ OpeningFcn’, @untitled_ OpeningFcn, …
‘gui_ OutputFcn’,@untitled_ OutputFcn, …
‘gui_ LayoutFcn’,[ ], …
‘gui_ Callback’, [ ] ) ;
if nargin && ischar (varargin{1})
gui_State. gui_ Callback = str2func (varargin{1});
end
if nargout
= gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function untitled_ OpeningFcn (hObject, eventdata, handles, varargin)
= uigetfile (‘*.amp’ , ‘sample Dialog Box’);
handles.output = hObject;
guidata( hObject,handles);
function varargout = untitled_ OutputFcn ( hObject, eventdata, handles )
varargout {1} =handles.output;
function A_Callback( hObject, eventdata, handles )
A_state=get(hObject, ‘ Value ’);
if A_state = = get(hObject, ‘ Max ’)
A = str2double (get(handles.A, ‘String’));
end
function B_Callback( hObject, eventdata, handles )
B_state=get(hObject, ‘ Value ’);
if B_state = = get(hObject, ‘ Max ’)
B = str2double (get(handles.B, ‘String’));
end
function C_Callback( hObject, eventdata, handles )
C_state=get(hObject, ‘ Value ’);
if C_state = = get(hObject, ‘ Max ’)
C = str2double (get(handles.C, ‘String’));
end
function D_Callback ( hObject, eventdata, handles )
D_state=get(hObject, ‘ Value ’);
if D_state = = get(hObject, ‘ Max ’)
D = str2double (get(handles.D, ‘String’));
end
function E_Callback ( hObject, eventdata, handles )
E_state=get(hObject, ‘ Value ’);
if E_state = = get(hObject, ‘ Max ’)
E = str2double (get(handles.E, ‘String’));
end
function F_Callback ( hObject, eventdata, handles )
F_state=get(hObject, ‘ Value ’);
if F_state = = get(hObject, ‘ Max ’)
F = str2double (get(handles.F, ‘String’));
end
function edit_Callback ( hObject, eventdata, handles )
function edit_CreateFcn ( hObject, eventdata, handles )
if ispc && isequal (get(hObject, ‘ BackgroundColor ’), get
(0, ‘ defaultUicontrolBackgroundColor ’))
set (hObject, ‘ BackgroundColor ’, ‘white’ );
end
function read_Callback ( hObject, eventdata, handles )
read_state=get(hObject, ‘ Value ’);
if read_state = = get(hObject, ‘ Max ’)
fidaus=fopen(’18.amp’,’w’)
fprintf(fidaus,’%f%f%f%f%f%f\n’.A, B, C, D, E, F)
end 还有我现在的程序运行以后会出现以下的错误:
fidaus=
1
??? Undefined function or variable 'A'.
Error in ==>read_Callback
fprintf(fidaus,’%f%f%f%f%f%f\n’.A, B, C, D, E, F)
Error in ==>gui_mainfcn
feval(varargin{:});
Error in ==>
gui_mainfcn(gui_State,varargin{:});
??? Error while evaluating uicontrol Callback
还有我现在用了uigetfile来引入文件,这样会弹出一个对话框,但我的本意是想用edittext来输入文件路径的,进而读入文件内容,而不想弹出对话框进行选择,但我不知道
function edit_Callback ( hObject, eventdata, handles )
function edit_CreateFcn ( hObject, eventdata, handles )
这2个函数应该怎么编,即我怎样通过edittext中的文件路径来读入文件的内容,还请高手指教.
界面见前面,恳请高手帮忙,万分感谢! 原帖由 wrx 于 2007-5-24 02:46 发表 http://www.chinavib.com/forum/images/common/back.gif
还有我现在用了uigetfile来引入文件,这样会弹出一个对话框,但我的本意是想用edittext来输入文件路径的,进而读入文件内容,而不想弹出对话框进行选择,但我不知道
function edit_Callback ( hObject, eventdata, handles )
function edit_CreateFcn ( hObject, eventdata, handles )
这2个函数应该怎么编,即我怎样通过edittext中的文件路径来读入文件的内容,还请高手指教.
请留意这两个函数的前后注释,那里有具体的说明 fidaus=
1
??? Undefined function or variable 'A'.
Error in ==>read_Callback
fprintf(fidaus,’%f%f%f%f%f%f\n’.A, B, C, D, E, F)
Error in ==>gui_mainfcn
feval(varargin{:});
Error in ==>
gui_mainfcn(gui_State,varargin{:});
??? Error while evaluating uicontrol Callback
参数传递问题先看看基础书吧,或者看看本版 置顶贴:聚宝盆,里面有个精华贴,建议仔细阅读,还有本版关于 GUI 的精华贴 最近看了些基础书,自己改了改程序,但还是出了些错误,恳请高手们帮我看看.另外我在自己编的部分加了些注释.
function varargout = untitled (varargin )
gui_Singleton = 1;
gui_State = struct (‘gui_Name’ , mfilename , …,
‘gui_ Singleton ’,gui_ Singleton, …
‘gui_ OpeningFcn’, @untitled_ OpeningFcn, …
‘gui_ OutputFcn’,@untitled_ OutputFcn, …
‘gui_ LayoutFcn’,[ ], …
‘gui_ Callback’, [ ] ) ;
if nargin && ischar (varargin{1})
gui_State. gui_ Callback = str2func (varargin{1});
end
if nargout
= gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function untitled_ OpeningFcn (hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.in.Pfad =’ F:\ajh\hong\bas.amp’;
set ( handles.editPfad, ‘string’, handles.in.Pfad);
guidata( hObject,handles);
function varargout = untitled_ OutputFcn ( hObject, eventdata, handles )
varargout {1} =handles.output;
这是在edittext中考进路径后执行打开该文件和创建新文件的操作。
function editPfad_Callback ( hObject, eventdata, handles )
handles.in.Pfad = get (hObject,’String’);
global Zahl
fidein = fopen(in.Pfad,’r’);
fidaus = fopen(’19.dat’,’w’);
m=6
n=1
while~feof(didein)
inputtext = fgetl(fidein)
if~isempty(inputtext)
Zahl = sscanf (inputtext,’%f’,) (这是把每一行的元素都写进一个矩阵,以后每选中一个Checkbox就读出相应的一个元素。)
=size(Zahl)
if(k~=0)&&(1~=0)
以下是通过Checkbox来选择读进文件的哪几列
function A_Callback( hObject, eventdata, handles )
global Spalte1;
A_state=get(hObject, ‘ Value ’);
if A_state = = get(hObject, ‘ Max ’) (如果这个Checkbox被选中。就把该列的元素读进变量Spalte1)
Spalte1 = Zahl(1,1);
end
function B_Callback( hObject, eventdata, handles )
global Spalte2;
B_state=get(hObject, ‘ Value ’);
if B_state = = get(hObject, ‘ Max ’)
Spalte2 = Zahl(2,1);
end
function C_Callback( hObject, eventdata, handles )
global Spalte3;
C_state=get(hObject, ‘ Value ’);
if C_state = = get(hObject, ‘ Max ’)
Spalte3 = Zahl(3,1);
end
function D_Callback ( hObject, eventdata, handles )
global Spalte4;
D_state=get(hObject, ‘ Value ’);
if D_state = = get(hObject, ‘ Max ’)
Spalte4 = Zahl(4,1);
end
function E_Callback ( hObject, eventdata, handles )
global Spalte5;
E_state=get(hObject, ‘ Value ’);
if E_state = = get(hObject, ‘ Max ’)
Spalte5 = Zahl(5,1);
end
function F_Callback ( hObject, eventdata, handles )
global Spalte6;
F_state=get(hObject, ‘ Value ’);
if F_state = = get(hObject, ‘ Max ’)
Spalte6 = Zahl(6,1);
end
function editPfad_CreateFcn ( hObject, eventdata, handles )
if ispc && isequal (get(hObject, ‘ BackgroundColor ’), get
(0, ‘ defaultUicontrolBackgroundColor ’))
set (hObject, ‘ BackgroundColor ’, ‘white’ );
end
以下是界面中最底下的那个键,即一按那个键,所选中的列的元素就写进了新的文件了。
function read_Callback ( hObject, eventdata, handles )
read_state=get(hObject, ‘ Value ’);
if read_state = = get(hObject, ‘ Max ’)
fprintf(fidaus,’%f%f%f%f%f%f\n’, Spalte1, Spalte2, Spalte3, Spalte4, Spalte5, Spalte6)
end
end
end 这是与function editPfad_Callback中的if(k~=0)&&(1~=0)相对
end 这是与function editPfad_Callback中的if~isempty(inputtext)相对
end 这是与function editPfad_Callback中的while~feof(didein)相对
fclose(fidein)
fclose(fidaus)
end 这是与function editPfad_CreateFcn相对
end 这是与function editPfad_Callback相对
但还是有以下的错误:就是function A_Callback作为子函数不对,我实在不知道问题在哪,请高手指导,万分感谢!!! 原帖由 wrx 于 2007-5-28 22:27 发表 http://www.chinavib.com/forum/images/common/back.gif
最近看了些基础书,自己改了改程序,但还是出了些错误,恳请高手们帮我看看.另外我在自己编的部分加了些注释.
function varargout = untitled (varargin )
gui_Singleton = 1;
gui_State = struct (‘gui_Name’ ...
matlab 不支持函数的嵌套定义,m 文件中如果存在多个函数,其关系是并列的 那得用函数间的调用吗?这个界面的执行是有先后关系的,我该怎么处理呀?请高手指教!谢谢! 原帖由 wrx 于 2007-5-29 01:10 发表 http://www.chinavib.com/forum/images/common/back.gif
那得用函数间的调用吗?这个界面的执行是有先后关系的,我该怎么处理呀?请高手指教!谢谢!
不是调用(即运行时候)的问题,是定义(即编程时候)的问题 不好意思,我不太明白你的意思,函数体是在你做好界面后自动生成的,而当几个函数体之间有相互关系时,我到底在编程的时候应该怎么处理? 恳请高手指教,万分感谢!!! 原帖由 wrx 于 2007-5-29 17:03 发表 http://www.chinavib.com/forum/images/common/back.gif
不好意思,我不太明白你的意思,函数体是在你做好界面后自动生成的,而当几个函数体之间有相互关系时,我到底在编程的时候应该怎么处理? 恳请高手指教,万分感谢!!!
在一个m文件中,正确的写法:
function a
....
function b
.....
错误的写法:
function a
...
if x == 1
function b
....
end
页:
[1]