You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How do I show rows and columns from a matrix in a edit texted in MATLAB?
7 views (last 30 days)
Show older comments
Hello everyone,
I'm still working on my project and I'm stuck again, please someone if could help me with thiss problem...
I want to show a matrix that contains a lot of columns and rows in a GUI and I don't know how to arrange that.
This is the matrix in the comand window: https://imgur.com/a/NeAYzXW
And this is how its show me in the interface: https://imgur.com/a/BR7aoSH
And this is the code:
MAIN PROGRAM:
s3 = ['[',regexprep(num2str(Vc),' +',' '),']'];
interfVc(s3);
INTERFACE PROGRAM:
function interfVc(s3)
%Manipularea figurii
figure('Name','CODARE/DECODARE TORNADO',...
'Units','normalized',...
'Color','white',...
'Position',[0.1,0.1,0.8,0.8]);
%'Elementele matricii de codare liniara Tornado [Vc]'
uicontrol('Style','Text',...
'Units','Normalized',...
'Position',[0.11,0.9,0.35,0.06],...
'BackgroundColor',[1,1,1],...
'FontSize',12,...
'FontWeight','b',...
'String','Elementele matricii de codare liniara Tornado [Vc]');
uicontrol('Style','Edit',...
'Units','Normalized',...
'Position',[0.11,0.4,0.7,0.5],...
'FontSize',10,...
'String',s3,...
'Callback','Vc=str2num(get(gco,''String''));interfVc(s3)');
Thank you.
Accepted Answer
Adam Danz
on 13 Aug 2019
Your matrix does not contains many rows. In fact, it's a row vector so it only contains 1 row. You can see this in your first image. Also this line would break if Vc had multiple rows: s3 = ['[',regexprep(num2str(Vc),' +',' '),']'];
So, the row vector is appearing correctly. If you'd like to reshape the vector into a matrix, please describe those intentions.
21 Comments
Demeter Andreea
on 13 Aug 2019
Oh, I see.
Then how do I arrange that vector with one row to showing me all the elements from that columns in that window interface?
Can I do that ?
Adam Danz
on 13 Aug 2019
That's a design question. Exactly how many elements are in that vector ( numel(Vc) ) ? How will you be using that display? Are your intentions that the user will copy-paste the value? Or just view the value?
If there are 10s or 100s of elements, do you really need to display the value at all?
I can help make suggestions if I know more about what your plans are.
Demeter Andreea
on 13 Aug 2019
Yes, u're right, it's more a design question, I want to visualize all that values in that edit text from that window
So, I'm making a program Encoding/Decoding Tornado
The Vc vector is the result from Encoding Tornado and I just want it to view the value
I wanted to make this by pushing a button from the main interface and to open another to show that values.
But I have problems with the call back button too now
When I push the button from down, that black button, I want to show the second window:
The main interface:
https://imgur.com/Tc4VsE0
Adam Danz
on 13 Aug 2019
Edited: Adam Danz
on 13 Aug 2019
Exactly how many elements are in that vector ( numel(Vc) ) ?
Do you want to show it as a row vector? Do you want to collapse the rows and put it in matrix form (examples below)?
% vector form
0 1 0 0 0 0 0 0 1 1 1 0 1 1 0 0 0 0
% matrix form
0 1 0 0 0 0
0 0 1 1 1 0
1 1 0 0 0 0
How will you show this value? In a text box (app designer?) ?
How will this display be used? Just for the user to see? or will the value from the text box be used somewhere else?
All of these questions need addressed. It still really unclear to give useful advice.
Demeter Andreea
on 13 Aug 2019
I think maximum values in Vc is like maybe 300-400.
I was thinkin' to show like 40 or 50 values on a row like u did in this example.
Vc it will always be a vector with one row and multiple columns, I just wanted display all the values more clearly, so I was thinkin to show them on more rows in that text box.
Vc is the final result from encoding.
And I have one more problem, I don't know to make the call back from that push button that I showed u, to get me to this figure with the values of Vc. I wanted to push that button, and to show me that figure with the values of Vc.
Maybe I will use Vc only to show some graphs in the right on the main interface.
Adam Danz
on 13 Aug 2019
Edited: Adam Danz
on 13 Aug 2019
I can help you with any of those ideas. I still don't know how these data will be used so I can't make any decisions for you. In your example, the vector only contained 1s and 0s. This is what that would look like in a text box and in a plot and I still can't imagine how that would be useful at all. Once you have a solid idea of how you want the Va vector to be represented, please describe it in detail and I can help you move forward with that.
% 400 values Scroll -------------------->
1 0 0 1 1 0 1 1 0 1 1 1 0 0 0 1 0 0 1 0 1 0 0 1 1
0 0 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0
0 1 0 1 0 0 0 1 1 1 1 0 1 1 0 1 1 0 0 1 1 1 1 1 0
0 0 1 1 1 1 0 0 0 1 1 1 1 0 1 1 0 1 0 1 1 0 1 1 0
0 1 1 0 0 1 0 1 0 1 1 0 1 1 1 0 1 1 0 0 0 0 1 0 0
1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1 1 1 0 0 0 0 0 1 1
1 0 1 0 0 0 1 0 1 1 0 1 1 0 1 0 1 1 0 0 0 0 1 0 0
0 1 1 0 0 0 0 1 0 1 1 0 1 1 0 1 1 0 0 1 1 0 1 1 0
1 1 0 1 0 1 1 0 1 1 0 0 1 0 0 0 0 0 1 1 1 0 0 1 1
1 0 0 1 1 1 0 0 0 1 0 1 0 1 0 1 1 0 0 1 1 0 1 1 1
1 0 0 1 1 0 0 1 0 0 1 1 1 1 0 0 1 1 0 1 0 1 1 0 1
1 1 1 0 1 1 1 0 0 0 0 0 0 1 1 1 0 1 0 1 0 1 0 1 0
0 1 0 1 1 0 1 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 0 0 0
0 0 1 1 1 1 0 0 1 0 0 1 0 1 0 0 1 0 0 1 1 0 0 1 1
1 1 1 0 1 1 0 0 1 1 0 0 0 1 1 0 1 0 0 0 1 1 0 0 0
0 0 0 1 1 0 1 0 0 1 0 0 1 1 1 0 0 1 0 0 0 1 0 1 0
Demeter Andreea
on 13 Aug 2019
How did u sort that vector from up like that? Can u show me the code of that ?
Adam Danz
on 13 Aug 2019
Vc = randi(2,1,400)-1; % row vec of 400 random integers between 0,1
Vm = reshape(Vc, 50, []).'; % reshaped into 50 columns (see notes)
Notes
- Vm = reshape(Vc, m, n) turns the vector Vc into a matrix with m rows and n columns and the order of the elements in Vm are columnar. That means the Vc(1) is in Vm(1,1); Vc(2) is in Vm(1,2) and so on.
- m or n can be empty if you want Matlab to fill it in automatically. In my example, I want 50 rows and however-many columns.
- In order for you to show values in Vc by row instead of by column, I reshaped it so it has 50 rows and then I transposed the matrix so column-1 becomes row-1. The .' at the end is the transpose.
- If Vc does not have enough values to fit into an mxn matrix, pad the end with NaNs.
Demeter Andreea
on 13 Aug 2019
I see, thank you.
So I made this function:
function A=reshapevec(Vc,x)
% reshape an arbitrary length vector into a matrix, with n columns, putting in zeros for
% missing elements if length of v does not divide by x
% find number of elements, in input vector
numElements=length(Vc);
% divide and round up (ceil) to get required number of rows, m
numRows=ceil(numElements/x);
% determine total number of elements,needed in the desired m x n array
numNeeded=numRows*x;
% determine number of missing elements (how many are we short by)
numMissing=numNeeded-numElements;
% concatenate additional needed zero elements
Vc =[Vc(:); zeros(numMissing, 1)];
% now reshape (wrap columnwise) to get a matrix of the desired size
A=reshape(Vc,numRows,x);
end
And in the main file I put this:
A=reshapevec(Vc,50);
s3 = ['[',regexprep(num2str(A),' +',' '),']'];
interfVc(s3);
And it gives me this error:
Error using regexprep
The 'STRING' input must be either a char row vector or a cell array of char row vectors.
Error in mjfgkjsf (line 71)
s3 = ['[',regexprep(num2str(A),' +',' '),']'];
How can I change this ?
Demeter Andreea
on 13 Aug 2019
Yeah, but if I do that I will have the same warning from before:
Warning: Single line Edit Controls can not have multi-line text
Adam Danz
on 13 Aug 2019
Edited: Adam Danz
on 13 Aug 2019
Then don't use a single-line edit control.
I'm assuming you're putting the text into an Edit Box. The Edit Box has a property "max". set it to 2. That allows you to have more than 1 row of text.
Here's a complete demo you can run.
figure()
uicontrol('Style','Edit','Units','Normalize','position',[.1,.1,.5,.4],...
'Max',2,'String',num2str(magic(9)),'FontName', 'Consolas');
% use a monospaced (fixed width) font like Consolas
I have a feeling the design of your GUI still isnt' solid. For example, your edit box will probably need to be expanded to fit the text (if it fits at all). Try to tackle some of this stuff independently - you'll learn so much more that way.
If you have any left over questions I'd be glad to help out more tomorrow.
Demeter Andreea
on 13 Aug 2019
Thank you so much and sorry for disturbing u so much! Talk tomorrow.
Good Night!
Demeter Andreea
on 19 Aug 2019
Hello Adam, can u you give me the code for the plot that u show me here?
Demeter Andreea
on 19 Aug 2019
Thank you Adam,
I have one more question, for the moment, please help me if u can...
I have a button and when I push the button I want to show me that figure from up there that we talked about in the past. I don't know how to make the callback or the link.
This is the code from another file, this is where I made that figure:
%Figura Codare TORNADO
%--------------------------------------------------------------------------
figure()
set(gcf,'color','Black'); %setare background
%'Elementele matricii de codare liniara Tornado [Vc]'
uicontrol('Style','Text',...
'Units','Normalized',...
'Position',[0.1,0.6,0.5,0.4],...
'BackgroundColor',[0,0,0],...
'FontSize',12,...
'ForegroundColor','White',...
'FontWeight','b',...
'FontName','Consolas',...
'String','Elementele matricii de codare liniara Tornado [Vc]');
%Tabloul de valori
uicontrol('Style','Edit','Units','Normalize','position',[.1,.5,.6,.4],...
'Max',2,'String',num2str(Vc),'FontName', 'Consolas');
And this is the code from the pushbutton that is in another file, the main function for my interface:
%PushButton Vc
uicontrol('Style','PushButton',...
'Units','normalized',...
'BackgroundColor',[0.0,0.0,0.0],...
'FontSize',8,...
'FontWeight','b',...
'ForegroundColor','W',...
'FontName','Consolas',...
'String','Matrice codare [Vc]',...
'Position',[0.08,0.15,0.12,0.08],...
'Callback',@FiguraVc );
Adam Danz
on 19 Aug 2019
It looks like you're setting up the pushbutton correctly. Is the data being generated within the callback function or are you passing those data into the function?
The callback func should look something like this if you're generated the data within the func.
function FiguraVc(hObject, event)
fh = figure();
axh = axes();
Vc = randi(2,1,400)-1; %vector of random 1s and 0s
plot(axh,Vc)
ylim(axh,[-1,2])
end % <--- only if your other funcs end with 'end'
If you're passing data into the callback function, you need to change the callback definition, too.
uicontrol('Style','PushButton',...
. . .
'Callback',{@FiguraVc,data});
function FiguraVc(hObject, event, data)
. . .
end % <
Demeter Andreea
on 19 Aug 2019
Edited: Demeter Andreea
on 19 Aug 2019
This is my main interface https://imgur.com/VCGcC3H and when I push the button from down I want to display me this figure that I made it in another file: https://imgur.com/gR3mNGc
When I push the button it give's me this error, because I don t have a function of callback I think. I have just a script where I put that figure:
Attempt to execute SCRIPT FiguraVc as a function:
C:\Users\Andreea\Desktop\Cod_Matlab\FiguraVc.m
Error while evaluating UIControl Callback
% I reedited this post, I did what u told me, and I made a function from
% that FiguraVc script but its not showwing me the values now of the Vc cuz I get
% this error:
Undefined function or variable 'Vc'.
Error in FiguraVc (line 19)
'Max',2,'String',num2str(Vc),'FontName', 'Consolas');
Error while evaluating UIControl Callback
Adam Danz
on 19 Aug 2019
If your file is a script, you cannot execute it as a function. How are you involking the script?
Demeter Andreea
on 19 Aug 2019
So I made it a function now:
%Figura Codare TORNADO
%--------------------------------------------------------------------------
function FiguraVc(~, ~)
figure()
set(gcf,'color','Black'); %setare background
%'Elementele matricii de codare liniara Tornado [Vc]'
uicontrol('Style','Text',...
'Units','Normalized',...
'Position',[0.1,0.6,0.5,0.4],...
'BackgroundColor',[0,0,0],...
'FontSize',12,...
'ForegroundColor','White',...
'FontWeight','b',...
'FontName','Consolas',...
'String','Elementele matricii de codare liniara Tornado [Vc]');
%Tabloul de valori
uicontrol('Style','Edit','Units','Normalize','position',[.1,.5,.6,.4],...
'Max',2,'String',num2str(Vc),'FontName', 'Consolas');
end
This is the callback-ul:
%PushButton Vc
uicontrol('Style','PushButton',...
'Units','normalized',...
'BackgroundColor',[0.0,0.0,0.0],...
'FontSize',8,...
'FontWeight','b',...
'ForegroundColor','W',...
'FontName','Consolas',...
'String','Matrice codare [Vc]',...
'Position',[0.08,0.15,0.12,0.08],...
'Callback',{@FiguraVc} );
The problem now is that its now showing me this part from code
%Tabloul de valori
uicontrol('Style','Edit','Units','Normalize','position',[.1,.5,.6,.4],...
'Max',2,'String',num2str(Vc),'FontName', 'Consolas');
It's showing me this when I press the button:
I don t see the tables anymore with the values of Vc and its givin me this error:
Undefined function or variable 'Vc'.
Error in FiguraVc (line 19)
'Max',2,'String',num2str(Vc),'FontName', 'Consolas');
Error while evaluating UIControl Callback
Adam Danz
on 19 Aug 2019
Try to troubleshoot this and understand what the problems are. You're talented and with a little investigation I believe you can do this and you'll learn so much in that process. If you get an error, google that error and read about it to understand it. Use the matlab documentation to understand functions and their inputs/outputs. I can check back later to chip in, after doing some of my own work.
More Answers (0)
See Also
Categories
Find more on Workspace Variables and MAT-Files in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)