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

8 Ansichten (letzte 30 Tage)
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 Kommentare
C K Chan
C K Chan am 4 Apr. 2021
Thank you for your reply.
I want the GUI to programmatically populate the edit box.
Geoff Hayes
Geoff Hayes am 4 Apr. 2021
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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

DGM
DGM am 4 Apr. 2021
Bearbeitet: DGM am 4 Apr. 2021
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 Kommentare
DGM
DGM am 4 Apr. 2021
Bearbeitet: DGM am 4 Apr. 2021
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by