MATLAB Edit Text: Display more than 8 characters - matlab

I'm trying to display this number '16777215' in an Edit Text in MATLAB GUI but i end up getting this number '1.67772e+07'
This is the code for displaying the number in the textbox:
set(handles.edit7, 'string', max_count);
How can I fix this?
Thank you

I think you could sue sprintf function. For example:
mFigure = figure()
mTextBox = uicontrol('style','text', 'Position', [20 240 200 20])
set(mTextBox,'String', sprintf('%d', 16777215))

I was able to observe the same behaviour that you mentioned. When I wrapped max_count in the function to convert the number to a string, num2str, 16777215 appeared as expected:
set(handles.edit7, 'string', num2str(max_count));

Related

How to align xlabels and ylabels in subplot in MATLAB

I have the following code:
mean_h =[11.9877,13.3937,16.1717];
std_h = [12.5379,10.2732,10.8000];
subplot(2,1,1)
hold on
h = bar(1:3,mean_h,0.2);
errorbar(1:3,mean_h,std_h,'s','MarkerSize',10,...
'MarkerEdgeColor','red','MarkerFaceColor','red');
name = {'4 mics','9 mics','24 mics'};
set(gca,'XTick',[1 2 3],'XTickLabel',{'4 mics','9 mics','24 mics'});
set(gca,'fontsize', 21);
legend({'mean_{hor}', 'std_{hor}'});
grid on
xlabel(' 3 different subsets of horizontal microphone pair combinations of
microphone array 3');
ylabel('Mean/stds rmse`s [°]');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Mean and standard devaiation for vertical rmse's of microphone array 3
mean_v =[7.3022,11.3737,16.2675];
std_v =[6.2369,9.9813,10.3599];
subplot(2,1,2)
hold on
h = bar(1:3,mean_v,0.2);
errorbar(1:3,mean_v,std_v,'s','MarkerSize',10,...
'MarkerEdgeColor','red','MarkerFaceColor','red');
name = {'4 mics','9 mics','24 mics'};
set(gca,'XTick',[1 2 3],'XTickLabel',{'4 mics','9 mics','24 mics'});
set(gca,'fontsize', 21);
legend({'mean_{ver}', 'std_{ver}'});
grid on
xlabel('3 different subsets of vertical microphone pair combinations of
microphone array 3');
ylabel('Mean/stds rmse`s [°]');
Now when i plot these two subplots i am facing an alignment problem in xlabels and ylabels . They are not aligned. Can anybody help me how can i fix this problem. Thanks
They don't align if the graphs are different. In your case the numbers are different.
You can either do it by hand or use a text(my_x, my_y,'mylabel') and set it up properly. I wrote an example below.
a=1:10;
b=a.^2;
subplot(4,1,1)
plot(a,b)
ylabel(' long text')
subplot(4,1,2)
plot(a,a)
ylabel('long even long text')
subplot(4,1,3)
hold
plot(a,b)
text(0,50,'long text','HorizontalAlignment','center','VerticalAlignment','middle', ...
'FontSize',12,'Rotation',90)
subplot(4,1,4)
hold
plot(a,a)
text(0,5,'long even long text','HorizontalAlignment','center','VerticalAlignment','middle', ...
'FontSize',12,'Rotation',90)
giving the graph below
Notice the last two are aligned, but it was necessary to input the coordinates by hand as i did.
As a tip, you often ca use your data and move it by a fixed percentage (e.g., the minimum -10% graph total length), making it possible to 'automate' your script.
Another way is to use the Position property of the ylabel. But this is similar to the method described above. To do so use
t=ylabel('long text')
t =
Text ( long text) with properties:
String: ' long text'
FontSize: 11
FontWeight: 'normal'
FontName: 'Helvetica'
Color: [0.15 0.15 0.15]
HorizontalAlignment: 'center'
Position: [0.3333 50 -1]
%other things here too
%overwrite the property
% Here you have to put [X_position Y_position Z_position]
t.Position= [0.2 50 -1];
For that to work, every x/ylabel need its own name, or same name, but update the position before you call the command again.

Categorical histogram labels

I've created a categorical histogram :
h = histogram( categorical( emotions{startIndex:endIndex,:} ) )
h.Categories
ans =
1×5 cell array
{'ANGER'} {'CONTEMPT'} {'DISGUST'} {'JOY'} {'SADNESS'}
>> h.Values
ans =
164 26 18 191 1
But there doesn't seem to be a way to display labels (i.e.h.Values) on the histogram bars.
Following this post, I tried this:
text(h.Values, h.Categories, num2str(h.Values'), 'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Bottom' );
but I just get:
Error using text First two or three arguments must be numeric doubles.
but the problem is my x values are never going to be numeric, they're categorical. How to fix it?
For a reproducible example, this would do:
emotions = { 'JOY','JOY','JOY','JOY','JOY','JOY','JOY','JOY','JOY','JOY','ANGER','ANGER','CONTEMPT','CONTEMPT','CONTEMPT','JOY','ANGER','ANGER','ANGER','ANGER'}
emotions = emotions.';
t = cell2table(emotions)
histogram(categorical(emotions))
You have interchanged the first two input arguments of text and h.Categories is not numeric double which it needs to be (as the error message is suggesting you).
So the solution is:
emotions = {'JOY','JOY','JOY','JOY','JOY','JOY','JOY','JOY','JOY', ...
'JOY','ANGER','ANGER','CONTEMPT','CONTEMPT','CONTEMPT','JOY', ...
'ANGER','ANGER','ANGER','ANGER'};
% emotions = emotions.'; %No need of this line
% t = cell2table(emotions); %No need of this line
h = histogram(categorical(emotions));
text(1:numel(h.Values), h.Values, num2str(h.Values.'), ...
'HorizontalAlignment', 'Center', 'VerticalAlignment', 'Bottom');
Result:

Create slider with dynamic range based on user input matlab gui

Hi I have a quick question on how to solve this issue. Basically I would like to take the inputs from two edit text boxes and specify them as the min and max of the sliders range. I don't have a problem doing this but as soon as the min is entered the slider disappears because the min is now above the default slider value, which is 0. I understand that the error is because the value is no longer in the range of the min and max, and I want to fix this by updating the value to be above the min in the call back function for the min/max input text boxes. Is there a way I can update the default value to be above min so I can get past this error and actually use the slider?
Warning: 'slider' control cannot have a 'Value' outside of 'Min'-'Max' range
Control will not be rendered until all of its parameter values are valid
Here is what I have tried to do in the call back to the edit box that gets user input for the minimum for the slider:
function input_min_Callback(hObject, eventdata, handles)
value_min=str2double(get(hObject, 'String'));
if value_min > sliderValue_default
set(handles.Input_Transverse_Shear_Layer1, 'Value', value_min+1);
set(handles.Input_Transverse_Shear_Layer1, 'Min', value_min);
end
Any help would be greatly appreciated!
Thanks
Your code looks fine to me. You seem to be missing a guidata(hObject,handles)at the end to update the guidata so this could be the problem (unless it's there but you did not include it in your above snippet).
In any case here is a bit of code that looks very much like yours and works fine. Try it out so you might see what is wrong with yours...
function UpdateSliderMin(~)
clc
clear
close all
%// Create GUI elements and set default slide value
hFig = figure('Position',[200 200 200 300]);
sliderValue_default = 0;
handles.Slider = uicontrol('style', 'Slider', 'Min', -5, 'Max', 10, 'Value',sliderValue_default, 'Units','normalized','position', [0.08 0.3 0.08 0.6], 'callback', #(s,e) SliderCbk);
handles.Text_min = uicontrol('Style','text','String','Min','position', [60 230 40 20]);
handles.Edit_min = uicontrol('Style','edit','String',num2str(get(handles.Slider,'min')),'position', [100 230 40 20],'Callback',#(s,e) MinCallback);
handles.Text_max = uicontrol('Style','text','String','Max','position', [60 180 40 20]);
handles.Edit_max = uicontrol('Style','edit','String',num2str(get(handles.Slider,'max')),'position', [100 180 40 20]);
handles.Text_val = uicontrol('Style','text','String','value','position', [60 130 40 20]);
handles.Edit_val = uicontrol('Style','edit','String',num2str(get(handles.Slider,'value')),'position', [100 130 40 20]);
guidata(hFig,handles)
%// Callback of the edit box for the min value
function MinCallback
value_min=str2double(get(handles.Edit_min, 'String'));
if value_min > sliderValue_default
set(handles.Slider, 'Value', value_min+1);
set(handles.Slider, 'Min', value_min);
set(handles.Edit_val,'String',get(handles.Slider, 'Value'));
end
guidata(hFig,handles)
end
%// Slider callback. Just to check the value is updated correctly
function SliderCbk
CurrentValue = get(handles.Slider,'Value');
set(handles.Edit_val,'String',num2str(CurrentValue));
guidata(hFig,handles)
end
end
Screenshot of the initial GUI:
And after setting the minimum value to 5, which is above the current value of the slider:
the current value is updated to 6 as expected.
Hope that helps!

How to view a more than 1 lines of sentences to uipanel in matlab?

I wanna view the result of the "Detection" function.
In the "Detection" function there is "messeges" variable.
From the function, i want that all the sentences in messeges variable can be preview in my GUI esspecially in UIPANEL.
How to do it. I have made a Panel design in matlab with tag=uipanel1.
[messeges]=Detection(handles.citra1); %it's to call the Detection
function.
here is my UIPANEL CODE..
hp1=uipanel('Position', [100 100 700 500],...
'Title','UI Panel 1');
set(hp1, [messeges]);
but it cannot display the sentences from the messeges variable into the panel1 that i had made before..
There are errors messeges like this
??? Error using ==> set
There is no 'jumlah pasang pixel yang pada objek 13
adalah 1000' property in the 'uipanel' class.
Error in ==> deteksi2citra>pushbutton3_Callback at 124
set(hp1, [messeges]);
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> deteksi2citra at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
#(hObject,eventdata)deteksi2citra('pushbutton3_Callback',
hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
I have find the rellated topic but i cannot find the solution.
Please help me..
There are three major problems with your code.
You always have to set a property of an object to something in Matlab.
set(Object_Handle,'PropertyName1',PropertyValue1,...
'PropertyName2',PropertyValue2...)
Thus you might be able to write this set(hp1, 'String', messages); but never set(hp1, [messages]);
uipanel is just a container object, which means that it could contain other GUI objects. You could put a text or edit (See uicontrol) containing your string in the uipanel. But the uipanel itself does not have a 'String' property.
The position vector of uipanel is normalized by default. So all the position values must be between 0 and 1. See position vector here for more info.
Example of putting multi-line text in uipanel:
(Note that this code is a standalone or self consistent code (unlike GUIDE), thus you can just copy and paste this code and run it in matlab command window.)
str = sprintf('Your \n Multiline \n String ...');
hp1 = uipanel('Title','UI Panel 1',...
'Position', [.25 .1 .67 .67]);
uicontrol(...
'Parent', hp1,...
'Style','text',...
'Units', 'Normalized', 'Position', [0 0 1 1],...
'String', str);

Title for popup-menu in matlab

I've made a popup-menu in Matlab using uicontrol, instead of using GUIDE. Here is my code:
figure;
String = sprintf( '%d#', 1:5);
String(end) = [];
CString=regexp(String , '#' , 'split');
uicontrol('style','popupmenu' , ...
'String' , CString , ...
'Position' , [100,400,100,24]);
But I don't know how can I put a subject for the popup-menu.
If anyone knows I'll appreciate for your help.
Thank You in Advance
You need to add another ui object, probably text or edit beside the popup menu:
(I personally rather edit since it looks nicer)
txt_obj = uicontrol(...
'Style','edit',...
'HorizontalAlignment','right',...
'String', 'Something',...
'Position' , [0,400,100,24],...
'BackgroundColor', [.9 .9 .9],...
'Enable','inactive');
I encourage you to use normalized units for ease of coding (positioning actually!).
For more info see position property if uicontrol object and Positioning Figures (which has the same concept of positioning).
First, set a figure handle:
h = figure;
Then, set the name of the window:
set(h,'Name','This is my title text');