查看: 2303|回复: 10|关注: 0

[已解决] 求教GUI中可编辑文本框输入数据存储问题

[复制链接]

新手

12 麦片

财富积分


050


2

主题

9

帖子

0

最佳答案
  • 关注者: 1
本帖最后由 叫我胖虎啊 于 2018-3-1 11:45 编辑

     萌新小白提问,想在Matlab GUI中可编辑文本框输入数据,存储到excel里面。但是运行后点击保存excel里面不能存储数据,请各位大神帮忙解答一下。另外,如果我想录入多组数据,应该怎么设置循环录入保存呢?
  1. function varargout = sub1(varargin)% SUB1 MATLAB code for sub1.fig
  2. %      SUB1, by itself, creates a new SUB1 or raises the existing
  3. %      singleton*.
  4. %
  5. %      H = SUB1 returns the handle to a new SUB1 or the handle to
  6. %      the existing singleton*.
  7. %
  8. %      SUB1('CALLBACK',hObject,eventData,handles,...) calls the local
  9. %      function named CALLBACK in SUB1.M with the given input arguments.
  10. %
  11. %      SUB1('Property','Value',...) creates a new SUB1 or raises the
  12. %      existing singleton*.  Starting from the left, property value pairs are
  13. %      applied to the GUI before sub1_OpeningFcn gets called.  An
  14. %      unrecognized property name or invalid value makes property application
  15. %      stop.  All inputs are passed to sub1_OpeningFcn via varargin.
  16. %
  17. %      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
  18. %      instance to run (singleton)".
  19. %
  20. % See also: GUIDE, GUIDATA, GUIHANDLES

  21. % Edit the above text to modify the response to help sub1

  22. % Last Modified by GUIDE v2.5 28-Feb-2018 16:47:27

  23. % Begin initialization code - DO NOT EDIT
  24. gui_Singleton = 1;
  25. gui_State = struct('gui_Name',       mfilename, ...
  26.                    'gui_Singleton',  gui_Singleton, ...
  27.                    'gui_OpeningFcn', @sub1_OpeningFcn, ...
  28.                    'gui_OutputFcn',  @sub1_OutputFcn, ...
  29.                    'gui_LayoutFcn',  [] , ...
  30.                    'gui_Callback',   []);
  31. if nargin && ischar(varargin{1})
  32.     gui_State.gui_Callback = str2func(varargin{1});
  33. end

  34. if nargout
  35.     [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  36. else
  37.     gui_mainfcn(gui_State, varargin{:});
  38. end
  39. % End initialization code - DO NOT EDIT


  40. % --- Executes just before sub1 is made visible.
  41. function sub1_OpeningFcn(hObject, eventdata, handles, varargin)
  42. % This function has no output args, see OutputFcn.
  43. % hObject    handle to figure
  44. % eventdata  reserved - to be defined in a future version of MATLAB
  45. % handles    structure with handles and user data (see GUIDATA)
  46. % varargin   command line arguments to sub1 (see VARARGIN)
  47. % Choose default command line output for sub1
  48. handles.output = hObject;

  49. % Update handles structure
  50. guidata(hObject, handles);

  51. % UIWAIT makes sub1 wait for user response (see UIRESUME)
  52. % uiwait(handles.figure1);


  53. % --- Outputs from this function are returned to the command line.
  54. function varargout = sub1_OutputFcn(hObject, eventdata, handles)
  55. % varargout  cell array for returning output args (see VARARGOUT);
  56. % hObject    handle to figure
  57. % eventdata  reserved - to be defined in a future version of MATLAB
  58. % handles    structure with handles and user data (see GUIDATA)

  59. % Get default command line output from handles structure
  60. varargout{1} = handles.output;


  61. % --------------------------------------------------------------------
  62. function Untitled_1_Callback(hObject, eventdata, handles)
  63. % hObject    handle to Untitled_1 (see GCBO)
  64. % eventdata  reserved - to be defined in a future version of MATLAB
  65. % handles    structure with handles and user data (see GUIDATA)


  66. % --------------------------------------------------------------------
  67. function Untitled_2_Callback(hObject, eventdata, handles)
  68. % hObject    handle to Untitled_2 (see GCBO)
  69. % eventdata  reserved - to be defined in a future version of MATLAB
  70. % handles    structure with handles and user data (see GUIDATA)


  71. % --------------------------------------------------------------------
  72. function Untitled_3_Callback(hObject, eventdata, handles)
  73. % hObject    handle to Untitled_3 (see GCBO)
  74. % eventdata  reserved - to be defined in a future version of MATLAB
  75. % handles    structure with handles and user data (see GUIDATA)


  76. % --------------------------------------------------------------------
  77. function Untitled_8_Callback(hObject, eventdata, handles)
  78. % hObject    handle to Untitled_8 (see GCBO)
  79. % eventdata  reserved - to be defined in a future version of MATLAB
  80. % handles    structure with handles and user data (see GUIDATA)


  81. % --------------------------------------------------------------------
  82. function Untitled_9_Callback(hObject, eventdata, handles)
  83. % hObject    handle to Untitled_9 (see GCBO)
  84. % eventdata  reserved - to be defined in a future version of MATLAB
  85. % handles    structure with handles and user data (see GUIDATA)


  86. % --------------------------------------------------------------------
  87. function Untitled_6_Callback(hObject, eventdata, handles)
  88. % hObject    handle to Untitled_6 (see GCBO)
  89. % eventdata  reserved - to be defined in a future version of MATLAB
  90. % handles    structure with handles and user data (see GUIDATA)


  91. % --------------------------------------------------------------------
  92. function Untitled_7_Callback(hObject, eventdata, handles)
  93. % hObject    handle to Untitled_7 (see GCBO)
  94. % eventdata  reserved - to be defined in a future version of MATLAB
  95. % handles    structure with handles and user data (see GUIDATA)


  96. % --------------------------------------------------------------------
  97. function Untitled_4_Callback(hObject, eventdata, handles)
  98. % hObject    handle to Untitled_4 (see GCBO)
  99. % eventdata  reserved - to be defined in a future version of MATLAB
  100. % handles    structure with handles and user data (see GUIDATA)


  101. % --------------------------------------------------------------------
  102. function Untitled_5_Callback(hObject, eventdata, handles)
  103. % hObject    handle to Untitled_5 (see GCBO)
  104. % eventdata  reserved - to be defined in a future version of MATLAB
  105. % handles    structure with handles and user data (see GUIDATA)



  106. function edit1_Callback(hObject, eventdata, handles)
  107. % hObject    handle to edit1 (see GCBO)
  108. % eventdata  reserved - to be defined in a future version of MATLAB
  109. % handles    structure with handles and user data (see GUIDATA)
  110. % Save the new density value
  111. % Hints: get(hObject,'String') returns contents of edit1 as text
  112. %        str2double(get(hObject,'String')) returns contents of edit1 as a double


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

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



  123. function edit2_Callback(hObject, eventdata, handles)
  124. % hObject    handle to edit2 (see GCBO)
  125. % eventdata  reserved - to be defined in a future version of MATLAB
  126. % handles    structure with handles and user data (see GUIDATA)
  127. if isnan(a1)
  128.     set(hObject, 'String', 0);
  129.     errordlg('Input must be a number','Error');
  130. end

  131. % Hints: get(hObject,'String') returns contents of edit2 as text
  132. %        str2double(get(hObject,'String')) returns contents of edit2 as a double


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

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



  143. function edit3_Callback(hObject, eventdata, handles)
  144. % hObject    handle to edit3 (see GCBO)
  145. % eventdata  reserved - to be defined in a future version of MATLAB
  146. % handles    structure with handles and user data (see GUIDATA)
  147. if isnan(a2)
  148.     set(hObject, 'String', 0);
  149.     errordlg('Input must be a number','Error');
  150. end


  151. % Hints: get(hObject,'String') returns contents of edit3 as text
  152. %        str2double(get(hObject,'String')) returns contents of edit3 as a double


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

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



  163. function edit4_Callback(hObject, eventdata, handles)
  164. % hObject    handle to edit4 (see GCBO)
  165. % eventdata  reserved - to be defined in a future version of MATLAB
  166. % handles    structure with handles and user data (see GUIDATA)
  167. if isnan(a3)
  168.     set(hObject, 'String', 0);
  169.     errordlg('Input must be a number','Error');
  170. end

  171. % Hints: get(hObject,'String') returns contents of edit4 as text
  172. %        str2double(get(hObject,'String')) returns contents of edit4 as a double


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

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



  183. function edit5_Callback(hObject, eventdata, handles)
  184. if isnan(a4)
  185.     set(hObject, 'String', 0);
  186.     errordlg('Input must be a number','Error');
  187. end
  188. % hObject    handle to edit5 (see GCBO)
  189. % eventdata  reserved - to be defined in a future version of MATLAB
  190. % handles    structure with handles and user data (see GUIDATA)

  191. % Hints: get(hObject,'String') returns contents of edit5 as text
  192. %        str2double(get(hObject,'String')) returns contents of edit5 as a double


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

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


  203. % --- Executes on button press in pushbutton2.
  204. function pushbutton2_Callback(hObject, eventdata, handles)
  205. name=get(handles.edit1,'String');%获取车站名称
  206. a1= str2double(get(handles.edit2, 'String'));%将指标1文本转为数值
  207. a2= str2double(get(handles.edit3, 'String'));%将指标2文本转为数值
  208. a3= str2double(get(handles.edit4, 'String'));%将指标3文本转为数值
  209. a4= str2double(get(handles.edit5, 'String'));%将指标4文本转为数值
  210. s={'name','a1','a2','a3','a4'};
  211. xlswrite('sub1.xlsx',s,1,'A1:A5')
复制代码


     下面是excel中保存的内容
1.png
     







MATLAB 基础讨论
版块优秀回答者

入门

108 麦片

财富积分


50500


0

主题

116

帖子

16

最佳答案
  • 关注者: 4
发表于 2018-3-1 12:29:50 | 显示全部楼层
本帖最后由 wx风之印 于 2018-3-1 18:03 编辑

没太明白你的意思,是像下面这样么?
无标题.png

demo1.m

3.92 KB, 下载次数: 9

demo1.fig

2.2 KB, 下载次数: 5

新手

12 麦片

财富积分


050


2

主题

9

帖子

0

最佳答案
  • 关注者: 1
 楼主| 发表于 2018-3-1 15:44:09 | 显示全部楼层
本帖最后由 叫我胖虎啊 于 2018-3-1 15:50 编辑
wx风之印 发表于 2018-3-1 12:29
没太明白弄你的意思,是像下面这样么?

对的,是这个意思。但是我想实现的是多个可编辑文本框的同步保存,这样多个既有字符串又有数字,然后写到excel,我帖子里的代码是先保存到一个元胞数组中。此外我还想实现一个功能就是第一列excel保存好之后,后面可以继续输入,然后保存到第二列,以此类推

MATLAB 基础讨论
版块优秀回答者

入门

108 麦片

财富积分


50500


0

主题

116

帖子

16

最佳答案
  • 关注者: 4
发表于 2018-3-1 16:00:49 | 显示全部楼层
叫我胖虎啊 发表于 2018-3-1 15:44
对的,是这个意思。但是我想实现的是多个可编辑文本框的同步保存,这样多个既有字符串又有数字,然后写到e ...

是每个文本框数据单独一列?
是的话可以这样写:
str1=get(handles.edit1,'string');
str2=get(handles.edit2,'string');
str3=get(handles.edit3,'string');

xlswrite('数据.xls',str1,'sheet1','A1');
xlswrite('数据.xls',str2,'sheet1','B1');
xlswrite('数据.xls',str3,'sheet1','C1');

MATLAB 基础讨论
版块优秀回答者

入门

108 麦片

财富积分


50500


0

主题

116

帖子

16

最佳答案
  • 关注者: 4
发表于 2018-3-1 16:20:01 | 显示全部楼层
叫我胖虎啊 发表于 2018-3-1 15:44
对的,是这个意思。但是我想实现的是多个可编辑文本框的同步保存,这样多个既有字符串又有数字,然后写到e ...

还是没太明白,要不你发一下往文本框里输入的数据,和exce中希望存储的内容

新手

12 麦片

财富积分


050


2

主题

9

帖子

0

最佳答案
  • 关注者: 1
 楼主| 发表于 2018-3-1 16:27:46 | 显示全部楼层
wx风之印 发表于 2018-3-1 16:20
还是没太明白,要不你发一下往文本框里输入的数据,和exce中希望存储的内容 ...


                               
登录/注册后可看大图

大概就是这个意思,我每次点击保存,都能生成一列,然后这样重复录入
还有就是写入的函数是放在edit这个控件的callba里面还是放在pushbutton里面呢?
万分感谢
123.png

MATLAB 基础讨论
版块优秀回答者

入门

108 麦片

财富积分


50500


0

主题

116

帖子

16

最佳答案
  • 关注者: 4
发表于 2018-3-1 17:00:09 | 显示全部楼层
叫我胖虎啊 发表于 2018-3-1 16:27
大概就是这个意思,我每次点击保存,都能生成一列,然后这样重复录入
还有就是写入的函数是放在edit这个 ...

函数写在pushbutton的callback里

str={};
try
     [ndata, text, str]=xlsread('data1.xls','sheet1');
catch
end
[row,col]=size(str);
str(1,col+1)=get(handles.edit1,'string');
str(2,col+1)=get(handles.edit2,'string');
str(3,col+1)=get(handles.edit3,'string');
str(4,col+1)=get(handles.edit4,'string');
str(5,col+1)=get(handles.edit5,'string');
assignin('base','str',str)
xlswrite('data1.xls',str,'sheet1');

新手

12 麦片

财富积分


050


2

主题

9

帖子

0

最佳答案
  • 关注者: 1
 楼主| 发表于 2018-3-2 10:05:51 | 显示全部楼层
wx风之印 发表于 2018-3-1 17:00
函数写在pushbutton的callback里

str={};

谢谢大佬,我把您的这个代码加在callback里面了,但是总会报错

出错 sub1 (line 42)
    gui_mainfcn(gui_State, varargin{:});

出错
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)sub1('pushbutton_Callback',hObject,eventdata,guidata(hObject))
计算 UIControl Callback 时出错。

MATLAB 基础讨论
版块优秀回答者

入门

108 麦片

财富积分


50500


0

主题

116

帖子

16

最佳答案
  • 关注者: 4
发表于 2018-3-2 10:58:33 | 显示全部楼层 |此回复为最佳答案
叫我胖虎啊 发表于 2018-3-2 10:05
谢谢大佬,我把您的这个代码加在callback里面了,但是总会报错

出错 sub1 (line 42)

昨天把括号打错了,应该为大括号:str{1,col+1}=get(handles.edit1,'string');
str{2,col+1}=get(handles.edit2,'string');
str{3,col+1}=get(handles.edit3,'string');
str{4,col+1}=get(handles.edit4,'string');
str{5,col+1}=get(handles.edit5,'string');
附件是我运行的没错误

demo3.fig

3.35 KB, 下载次数: 12

demo3.m

7.82 KB, 下载次数: 11

新手

12 麦片

财富积分


050


2

主题

9

帖子

0

最佳答案
  • 关注者: 1
 楼主| 发表于 2018-3-2 11:07:26 | 显示全部楼层
wx风之印 发表于 2018-3-2 10:58
昨天把括号打错了,应该为大括号:str{1,col+1}=get(handles.edit1,'string');
str{2,col+1}=get(handles.e ...

太感谢了,完美运行,真心感谢,大佬元宵节快乐
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐上一条 /3 下一条

快速回复 返回顶部 返回列表