How to Convert MATLAB GUI Edit Text to Matrix?

In summary, to create a matrix from the values in 30 edit texts, you can use a loop to store the values into a vector and then use the reshape function to convert the vector into a matrix.
  • #1
windapradina
1
0
0 down vote favorite
share [g+] share [fb] share [tw]


Hi...I'm the beginner. I want to ask, how to put edit text to matrix? For example,
I have 30 edit text that will fill by number 0 - 1.
I want to make matrix x(1,1),...x(1,30) from the input of edit text.

I have tried this code :

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'))


till...

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'))

but, the command window show like this...

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

but actually I want the result is matrix, like

1 0 0 0.25 0.5 0

Does anyone know how to solve this problem?
 
Physics news on Phys.org
  • #2
One way to solve this problem is to store the values from the edit texts into a vector instead of a matrix. You can do this by using a loop that reads in the values from each edit text and stores them into a vector. Once you have stored all the values, you can then use the reshape function to convert the vector into a matrix. For example, if you have 30 edit texts, your code could look something like this: vec = zeros(1,30); for i = 1:30 %loop through all the edit texts vec(i) = str2double(get(hObject,'string')); %convert edit text value to double and store it in vector endx = reshape(vec,1,30); %reshape vector into a matrix This should give you the desired result.
 

FAQ: How to Convert MATLAB GUI Edit Text to Matrix?

How do I convert the input from an edit text box to a matrix in MATLAB GUI?

To convert the input from an edit text box to a matrix in MATLAB GUI, you can use the str2num function. This function converts a string to a numerical array, which can then be used as a matrix in your code.

Can I use the input from multiple edit text boxes to create a multi-dimensional matrix in MATLAB GUI?

Yes, you can use the input from multiple edit text boxes to create a multi-dimensional matrix in MATLAB GUI. You can use the str2num function to convert each input into a numerical array, and then use those arrays to create a multi-dimensional matrix.

How do I pre-fill an edit text box with a default value in MATLAB GUI?

To pre-fill an edit text box with a default value in MATLAB GUI, you can use the 'String' property of the edit text box. Set this property to the desired default value and it will be displayed in the box when the GUI is opened.

Is it possible to restrict the input in an edit text box to only numerical values in MATLAB GUI?

Yes, it is possible to restrict the input in an edit text box to only numerical values in MATLAB GUI. You can use the 'Enable' and 'Callback' properties of the edit text box to limit the input to only numerical values and display an error message if the user enters non-numerical values.

Can I use the input from an edit text box to perform calculations in MATLAB GUI?

Yes, you can use the input from an edit text box to perform calculations in MATLAB GUI. After converting the input to a numerical array using the str2num function, you can use it in your code to perform calculations and display the results in the GUI.

Similar threads

Replies
1
Views
4K
Replies
5
Views
2K
Replies
2
Views
12K
Replies
2
Views
1K
Replies
2
Views
3K
Replies
4
Views
17K
Replies
1
Views
1K
Back
Top