0

我是初学者。我想问,如何将编辑文本放入矩阵?例如,我有 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

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

4

1 回答 1

0

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

一种方法是使用结构在函数之间传递x变量。handles只需使用handles.x而不是x. 我还建议handles.x = zeros(1,30);在 GUI 初始化时初始化这个矩阵。

于 2012-03-05T04:13:46.770 回答