How to display a vector in edit text in MATLAB?

Hello, I need to display a vector in edit text and I don't know how to do that, please, a little help if someone can...
This is my code:
From interface:
uicontrol('Style','Text',...
'Units','Normalized',...
'Position',[0.085,0.68,0.1,0.03],...
'BackgroundColor',[0.75,0.75,0.75],...
'FontSize',10,...
'FontWeight','b',...
'String','Vi[Vector intrare]:');
uicontrol('Style','Edit',...
'Units','Normalized',...
'Position',[0.09,0.63,0.05,0.03],...
'FontSize',10,...
'String',Vi,...
'Callback','Vi=str2num(get(gco,''String''));interfata(R,L,C,tip,Ver,Vi)');
In the main program:
Vi=[0 0 1 0 0 1 1 0 0 0 0 0];
interf(N,R,tip,Ver,Vi);

 Respuesta aceptada

Adam Danz
Adam Danz el 10 de Ag. de 2019
Editada: Adam Danz el 10 de Ag. de 2019
Vi=[0 0 1 0 0 1 1 0 0 0 0 0];
>> s = ['Vi[',regexprep(num2str(Vi),' +',' '),']']
s =
'Vi[0 0 1 0 0 1 1 0 0 0 0 0]'

8 comentarios

Demeter Andreea
Demeter Andreea el 10 de Ag. de 2019
OMG, thank you so much!
No problem!
I just updated the answer to get rid of the extra white space between numbers.
% Old version
s = ['Vi[',num2str(Vi),']']
% New version
s = ['Vi[',regexprep(num2str(Vi),' +',' '),']']
Demeter Andreea
Demeter Andreea el 10 de Ag. de 2019
Would u mind to answer me at one more question please: Can I change with another values in the edit text from the interface and it will work or I will always have that values that I put it in main program?
Adam Danz
Adam Danz el 10 de Ag. de 2019
I'm not sure I understand the question. Are you asking whether you can edit the text from the GUI by typing in other numbers? Sure!
Or are you asking whether you can change the text from within the code? That's also quite simple. Maybe I'm not understanding the question.
Demeter Andreea
Demeter Andreea el 10 de Ag. de 2019
Yes, I'm saying that if I will put another numbers in the edit text, if Vi will get that new values (numbers), because I will need to show a result that depends from Vi changes. But I will work on that later.
Adam Danz
Adam Danz el 10 de Ag. de 2019
Editada: Adam Danz el 10 de Ag. de 2019
I see. You want to pull the string out from the text box, isolate the vector, and convert that back to numeric values. We can do that.
However, I recommend if possible, only allowing numeric text in that field rahter than including the "Vi[...]". That way you don't have to parse the numeric values from the non-numeric values. It gives you better control and the user less opportunity to break the rules. Do you know what I mean?
In any case, here's how you would work with the string from your example.
s = 'Vi[0 0 1 0 0 1 1 0 0 0 0 0]';
tokens = regexp(s,'\[(.+)\]','tokens');
d = str2double(strsplit(tokens{:}{:}));
result
d =
0 0 1 0 0 1 1 0 0 0 0 0
% (numeric)
If you decide to only permit numeric text in the text box, it's a lot simpler
s = '0 0 1 0 0 1 1 0 0 0 0 0';
d = str2double(strsplit(s))
Demeter Andreea
Demeter Andreea el 10 de Ag. de 2019
Thank u for everything! U helped me a lot. :)
Adam Danz
Adam Danz el 10 de Ag. de 2019
Cu plăcere!

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 10 de Ag. de 2019
By default it appears the Edit control converts an array to a column array of values and you can't display that in one row...if the size isn't too long and you can afford the real estate on the screen, you could set the 'min/max' properties and display the rows.
To keep as a one-row string, you've got to do the text conversion yourself --
vstr=sprintf([repmat('%d,',1,numel(Vi)-1) '%d'],Vi);
hUIEd=uicontrol('Syle','Edit', ...
...
'String',vstr,
...);
and use it as the 'String'

Categorías

Etiquetas

Preguntada:

el 10 de Ag. de 2019

Comentada:

el 10 de Ag. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by