Display seven-segment

I wish to design a GUI to display seven-segment as shown as the figure below.
But I want to ask user to enter their input between range 0-9 in the command window but not in GUI. Which mean i will receive the input from command window and display it in the GUI. What code can make it work?

Respuestas (2)

Walter Roberson
Walter Roberson el 8 de Mayo de 2020

0 votos

Create a 10 x 7 logical array, one row for each digit, with each row being a list of values of which segments should be lit for the corresponding digit. You can use any order of segments that is consistent. For example the 6th row, for the digit 5, could be [true true false true false true true] .
Then when you have the user input and have verified that the input is a non-negative integer with a single digit, then take the input, add 1, and use that as the row index to get out the segment information. Then use that information to set the segments appropriately.
Or you could define it as an array of color letters, such as 'bbkbkbb' that you use to set the colors for the segment.

11 comentarios

Suk King Chia
Suk King Chia el 8 de Mayo de 2020
I see, may I know where should I put the array into my script? Since I cannot use the callback button, what function can replace it instead?
function varargout = HDTask2(varargin)
% UNTITLED MATLAB code for untitled.fig
% UNTITLED, by itself, creates a new UNTITLED or raises the existing
% singleton*.
%
% H = UNTITLED returns the handle to a new UNTITLED or the handle to
% the existing singleton*.
%
% UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED.M with the given input arguments.
%
% UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help untitled
% Last Modified by GUIDE v2.5 06-May-2020 02:42:39
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled_OpeningFcn, ...
'gui_OutputFcn', @untitled_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to untitled (see VARARGIN)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitled_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
function
Rik
Rik el 8 de Mayo de 2020
The OpeningFcn would make the most sense to me, since you aren't really using your GUI as a GUI.
For that same reason; why are you using a GUI in the first place? Why not use a less bloated function instead?
Rik
Rik el 8 de Mayo de 2020
How strange. It makes much more sense to have an edit field in your figure and have a button that intiates the proces. So in future you should do that.
For future reference: GUIDE will be removed from Matlab at some point in the future. If you want to learn other ways to create a GUI in Matlab this thread should be a good starting point.
Walter Roberson
Walter Roberson el 8 de Mayo de 2020
Yes, but a MATLAB figure is not the same as a GUIDE GUI.
What are "outputs a - g" that you are required to use logical operations to produce?
Suk King Chia
Suk King Chia el 8 de Mayo de 2020
@ Rik noted with thanks!
@ Walter Roberson the output a-g are from the truth table which describe the input-output relationship of the BCD to seven-segment decoder.
Walter Roberson
Walter Roberson el 8 de Mayo de 2020
Ah, in that case, the method I suggested of using a 10 x 7 array would not be appropriate. You need to implement some kind of truth table to decide which segments to light, apparently, based upon the A* variables.
For example, top segment is
A3 == 0 & A1 == 0 & A2 ~= A0
Rik
Rik el 9 de Mayo de 2020
None of those functions actually do anything. Why not move away from GUIDE and build your own? Then you don't need these 158 lines. The way I read your assignment it doesn't actually require GUIDE. Why teach yourself to use a tool that is broken and will be removed?
Suk King Chia
Suk King Chia el 9 de Mayo de 2020
It does require me use guide in matlab, even it will be remove from matlab but for now I still need use it to complete my homework btw thank you for ur suggestion!
Rik
Rik el 9 de Mayo de 2020
It might not be my place to criticize you teacher, but all these requirements seem very odd to me.
Walter Roberson
Walter Roberson el 9 de Mayo de 2020
"Create a graphical interface using a MATLAB program named HDTask2.m to verify your digital circuit design, which must also satisfy the following programming requirements:"
That does NOT require that you use guide.
Suk King Chia
Suk King Chia el 9 de Mayo de 2020
Cause my lecturer mentioned use guide before, but I will ask my lecturer is it compulsory to use guide or I can do this task in different way. Really appreciate ur guys help!

Iniciar sesión para comentar.

Image Analyst
Image Analyst el 9 de Mayo de 2020
Editada: Image Analyst el 9 de Mayo de 2020

0 votos

I think the simplest way is to just have 10 images. Then all you have to do is to convert the user input into a filename string and display the proper image. I'd have 7 edit text boxes where the user can type in a 0 or 1. In the callback for that edit field, you call a function to read all 7 edit fields and get the filename and read in and display the proper image. For example in each edit field's callback you'd have
function edtDigit1_Callback(hObject, eventdata, handles)
DecodeDigitsAndDisplayImage(handles);
Same for all the other 6 edit fields. Or, if you have to, get the 7 strings from the command window.
Now the DecodeDigitsAndDisplayImage() function would look like
function DecodeDigitsAndDisplayImage(handles)
% Get digits from edit fields on GUI.
digit1 = handles.edtDigit1.String;
digit2 = handles.edtDigit2.String;
% etc. for all 7 digits.
% Or, if you have to, get the 7 strings from the command window.
ourDigit = [digit1, digit2, digit3, digit4, digit5, digit6, digit7]
% Read in proper image, one of 10 or 11 (if including all off condition) images.
baseFileName = sprintf('%s.png', ourDigit); % Assuming it's a PNG image file that you have.
fullFileName = fullfile(pwd, baseFileName) % For example 'c:\test\0010110.png'
theImage = imread(fullFileName);
% Display that image.
imshow(theImage, []);
You don't really even need GUIDE for this if you're not using edit fields. If you're just getting 0s and 1s from the command window you could just get those digits somehow, like from input() or inputdlg(), and then call the function passing in those strings:
function DecodeDigitsAndDisplayImage(digit1, digit2, digit3, digit4, digit5, digit6, digit7)
% Build the filename from the 7 strings gotten from the command window.
ourDigit = [digit1, digit2, digit3, digit4, digit5, digit6, digit7]
% Read in proper image, one of 10 or 11 (if including all off condition) images.
baseFileName = sprintf('%s.png', ourDigit); % Assuming it's a PNG image file that you have.
fullFileName = fullfile(pwd, baseFileName) % For example 'C:\test\0010110.png'
theImage = imread(fullFileName);
% Display that image.
imshow(theImage, []);
Again, the 10 or 11 images have been computed in advance. You already have, and showed us, the 5 so you just need to fill or clear the other segments and save the images. If you don't want to save the images in advance, you could just have a function where you create the digits on the fly, though it's a bit more involved.

3 comentarios

Suk King Chia
Suk King Chia el 9 de Mayo de 2020
Thanks a lot! I will try this out!
michelle papas
michelle papas el 4 de Sept. de 2020
Hi did you get this program to work?
Image Analyst
Image Analyst el 4 de Sept. de 2020
Editada: Image Analyst el 4 de Sept. de 2020
Possibly not since he didn't accept my answer. But that doesn't mean you can't do it. Give it a try. Do you have the 10 images yet? If not, a few minutes in Photoshop and you'll have them created. I'm attaching a set here.

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 8 de Mayo de 2020

Editada:

el 4 de Sept. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by