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

조회 수: 8 (최근 30일)
C K Chan
C K Chan 2021년 4월 3일
편집: DGM 2021년 4월 4일
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
C K Chan
C K Chan 2021년 4월 4일
Thank you for your reply.
I want the GUI to programmatically populate the edit box.
Geoff Hayes
Geoff Hayes 2021년 4월 4일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

DGM
DGM 2021년 4월 4일
편집: DGM 2021년 4월 4일
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
DGM
DGM 2021년 4월 4일
편집: DGM 2021년 4월 4일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by