在MATLAB中将编辑文本输入放入矩阵中

时间:2012-03-05 00:28:20

标签: matlab text matlab-guide

我是初学者。我想问一下,如何将编辑文本放到矩阵中?例如,我有30个编辑文本,将按编号0 - 1填充。我想从编辑文本的输入中创建矩阵x(1,1),.... x(1,30)

我试过这段代码:

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)

% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double

x(1, 1) = str2double(get(hObject,'string'))

...直到

function edit30_Callback(hObject, eventdata, handles)

% hObject handle to edit30 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit30 as text
% str2double(get(hObject,'String')) returns contents of edit30 as a double

x(1, 30) = str2double(get(hObject,'string'))

但是,命令窗口显示如下......

x =

     1

x =

     0 0

x =

     0 0 0

x =

         0 0 0 0.2500

x =

         0 0 0 0 0.5000

x =

     0 0 0 0 0 0

但实际上我希望结果是矩阵,比如

1 0 0 0.25 0.5 0

有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

你的问题是功能范围。每个回调函数在其自己的作用域中定义x,因此当函数结束时(在控制台中显示x值),x将消失。

一种方法是使用x结构在函数之间传递handles变量。只需使用handles.x代替x即可。我还建议在GUI初始化时将此矩阵初始化为handles.x = zeros(1,30);