How to display a matrix form command window to GUI edit text box or table?

I've calculated a matrix in command and I want to display the matrix on GUI edit text box or table. How can I transfer the matrix to GUI edit box?

3 Comments

Do you want the GUI to programmatically populate the edit box or do you just want the user to be able to access the variables in the workspace by using their names?
Thank you for your reply.
I want the GUI to programmatically populate the edit box.
Are you using GUIDE, App Designer or programmatically creating your GUI? What are the dimensions of the matrix that you want to write to the edit text box?

Sign in to comment.

 Accepted Answer

Still not quite sure what you're building or how you want to prompt the behavior. This answer isn't going to be definitive and probably is nowhere near best practices.
If your GUI just needs to fetch a matrix from the base workspace and display it in a text box, something like this may get you started:
% get an array from the workspace
% if you're using a text box to get the variable name string 'myarray'
% this also allows you to do things like 'abs(myarray)+2' in the text box
copyofmyarray=evalin('base','myarray');
% update the textbox
set(handles.textboxwherematrixgoes,'string',mat2str(copyofmyarray))
This works for programmatically built GUIs. I've never used AppDesigner.

4 Comments

Thank you, it is worked.
I'm using GUIDE to create my GUI, I've got another problem with the matrix,
Here is my 4x4 matrix,
A =
0.9848 -0.1736 0 89.3923
0.0000 0.0000 -1.0000 0.0000
0.1736 0.9848 0.0000 26.9459
0 0 0 1.0000
the figure shows how it displys on the text box.
Is it any way to display it more like a 4x4 matrix?
I've never tried doing multiline text boxes, but it can be done. Getting the text to show up right might take some fiddling, but I imagine it can be done. You can convert the matrix to a multiline string sort of like this:
a=rand(4);
b=mat2str(a,4); % i'm going to use a lower precision as an example
c=replace(b,';','; ...\n'); % replace row breaks with line extension syntax
fprintf([c '\n']) % dump the string
Now if that matrix string needs to be user-edited and then read back as a numeric matrix, that might be another challenge.
I threw together a test gui made from scraps, and the prior answer didn't work for me. The issue is with how the multiline text boxes handle strings. By default it seems to produce a padded char matrix, which is a pain to use. If initialized with a cell array, it can handle cell arrays too. This is a bit more work than just expecting it to handle newlines naturally.
I set up my edit box:
uicontrol(...
'Parent',mainpanel,...
'Units','normalized',...
'BackgroundColor',[1 1 1],...
'HorizontalAlignment','left',...
'Position',[ehm tsp+0.015-20*eh 1 eh*20],... % ignore these geometry vars
'String',samplestring,... % this is a cell array of chars
'Style','edit',...
'max',20, 'min',10, ...
'Tag','sampradiusbox',...
'callback',@sampradiusboxCBF);
the callback function is
% dump everything to console to demonstrate what's going on
function sampradiusboxCBF(objh,~)
% just dump the cell array of strings as it comes from the box
samplestring=get(objh,'string')
% dump the result after converting it back to numeric
% of course this will be garbage if the text isn't an array
editedmatrix=str2num(horzcat(samplestring{:}))
end
I'm taking a numeric argument when I launch the GUI, and using that to populate the edit box after the UI is fully initialized.
% mymatrix is the input argument for the main function
b=mat2str(mymatrix,4);
c=replace(b,';','; ...');
samplestring=split(c,'...');
set(handles.sampradiusbox,'string',samplestring)
Calling the GUI with an input argument:
testgui(rand(4))
If I edit the first two values, this gets dumped to console:
samplestring =
4×1 cell array
{'[5 0 0.09871 0.1366; ' }
{'0.3955 0.8852 0.2619 0.7212; '}
{'0.3674 0.9133 0.3354 0.1068; '}
{'0.988 0.7962 0.6797 0.6538]' }
editedmatrix =
5.0000 0 0.0987 0.1366
0.3955 0.8852 0.2619 0.7212
0.3674 0.9133 0.3354 0.1068
0.9880 0.7962 0.6797 0.6538
It might be more practical to use something like a uitable object instead of a text box. It's probably less likely to break.

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Asked:

on 3 Apr 2021

Edited:

DGM
on 4 Apr 2021

Community Treasure Hunt

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

Start Hunting!