I am trying to write a program where the user inputs values to a 2x2 matrix into a GUI. When I try to get it to display the matrix however, it displays completely different numbers

2 visualizzazioni (ultimi 30 giorni)
Here is the code that I wrote:
function matrixinput
%Generate the actual window prompt
matrixwindow = figure;
set(matrixwindow, 'MenuBar', 'none',...
'ToolBar', 'none',...
'Name','Enter',...
'NumberTitle','off',...
'Position', [450 450 200 140])
%Generate the main "label" text
text = uicontrol(matrixwindow, 'Style','text',...
'String','Please enter the 2x2 matrix values',...
'Position',[0 100 200 30]);
%Add in the text editing fields
Top_Left = uicontrol('Style', 'edit',...
'Position',[70 80 25 25]);
Top_Right = uicontrol('Style', 'edit',...
'Position',[100 80 25 25]);
Bottom_Left = uicontrol('Style', 'edit',...
'Position',[70 50 25 25]);
Bottom_Right = uicontrol('Style', 'edit',...
'Position',[100 50 25 25]);
%Add in submit button
submitbtn = uicontrol('Style','pushbutton',...
'String','Submit',...
'Position',[50 10 100 30],...
'Callback', {@submitbtn_Callback, Top_Left, Top_Right, Bottom_Left, Bottom_Right});
end
function submitbtn_Callback(~, ~, Top_Left, Top_Right, Bottom_Left, Bottom_Right)
%Collect the input values and define variables:
a = num2str(get(Top_Left, 'String'));
b = num2str(get(Top_Right, 'String'));
c = num2str(get(Bottom_Left, 'String'));
d = num2str(get(Bottom_Right, 'String'));
%Input the values into matrix form:
A = [a, b;
c, d];
%Display matrix A:
disp(A)
disp(1*A)
end
Given this code, when I input the values: 1, 2, 3, 4; the command window prints this out:
>> rowreduction
12
34
49 50
51 52
When I ask the program to display each piece individually, it does it fine... but for some reason when I try to format it as a matrix, it does all sorts of weird things. Does anyone have any idea what I'm doing wrong? What really confuses me is the fact that disp(A) seems to print the correct thing... but it's not in a matrix format... and then disp(1*A) puts it in the correct format, but the numbers are all wrong.
(I'm really trying to build this program by myself without outside help... but I can't seem to figure out where the values of 49, 50, 51 and 52 are coming from... any help would be greatly appreciated!)

Risposte (2)

Star Strider
Star Strider il 4 Feb 2016
Those are the ASCII codes for the numbers. You need to convert them from strings to numbers.
The problem is that your ‘text’ variable is of the ‘matlab.ui.control.UIControl’ class, so you will have to figure out how to do the conversion operation on it.

Christian Guitart
Christian Guitart il 22 Feb 2016
Instead of num2str try with str2num function. Below the code modified lines:
a = str2num(get(Top_Left, 'String'));
b = str2num(get(Top_Right, 'String'));
c = str2num(get(Bottom_Left, 'String'));
d = str2num(get(Bottom_Right, 'String'));

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by