How can I assign the new variables with base variables?

2 vues (au cours des 30 derniers jours)
Xingmin LI
Xingmin LI le 10 Fév 2021
I am using app funtion to detect object.
Now I want to assign the object detected as a new variable to find its information in EXCEL.
I succesfully to detect the object by using the button pushed function of 'scanbutton'
but I do not know how to assign the object dected to the second button.
For example, I detect orange, and I want to know its calories by just click the nutrients&calories button. When I click the the'nutrients&calories', it will find its name in the first column, and read it calories from second column.
The detect object is a variable, it can be any things, just like orange, apple, pizza or any other things. Now what I can do is to read the infor of the given name, how can get the infor is the thing is a varibale?
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
SCANButton matlab.ui.control.Button
NutrientsCaloriesButton matlab.ui.control.Button
Label matlab.ui.control.Label
Label2 matlab.ui.control.Label
UIAxes matlab.ui.control.UIAxes
end
properties (Access = private)
Property
vidObj;
net;
inputsize;
newing;
CName; % Description
CName1; % Description
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.net=googlenet;
app.inputsize=app.net.Layers(1).InputSize;
app.vidObj=videoinput('macvideo');
preview(app.vidObj);
end
% Button pushed function: SCANButton
function SCANButtonPushed(app, event)
img=getsnapshot(app.vidObj);
img=ycbcr2rgb(img);
image(img,'Parent',app.UIAxes);
app.newing=imresize(img,app.inputsize(1:2));
app.CName=classify(app.net,app.newing);
CName0=char(app.CName);
system(sprintf('say %s', CName0));
app.Label.Text=app.CName;
end
% Button pushed function: NutrientsCaloriesButton
function NutrientsCaloriesButtonPushed(app, event)
FoodTable = readtable('foods.xlsx');
% Find all the rows that are the orange
app.CName = FoodTable(strcmp(FoodTable.Name, app.CName), :);
% Show the calories
fprintf(" CName has %d calories\n", app.CName1.Calories);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Color = [0.9294 0.6941 0.1255];
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create SCANButton
app.SCANButton = uibutton(app.UIFigure, 'push');
app.SCANButton.ButtonPushedFcn = createCallbackFcn(app, @SCANButtonPushed, true);
app.SCANButton.BackgroundColor = [0.651 0.651 0.651];
app.SCANButton.FontColor = [0.149 0.149 0.149];
app.SCANButton.Position = [148 432 100 22];
app.SCANButton.Text = 'SCAN';
% Create NutrientsCaloriesButton
app.NutrientsCaloriesButton = uibutton(app.UIFigure, 'push');
app.NutrientsCaloriesButton.ButtonPushedFcn = createCallbackFcn(app, @NutrientsCaloriesButtonPushed, true);
app.NutrientsCaloriesButton.BackgroundColor = [0.651 0.651 0.651];
app.NutrientsCaloriesButton.FontColor = [0.149 0.149 0.149];
app.NutrientsCaloriesButton.Position = [126 50 122 22];
app.NutrientsCaloriesButton.Text = 'Nutrients & Calories';
% Create Label
app.Label = uilabel(app.UIFigure);
app.Label.Position = [381 415 89 56];
% Create Label2
app.Label2 = uilabel(app.UIFigure);
app.Label2.Position = [381 50 42 22];
app.Label2.Text = 'Label2';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Picture of food')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [37 107 548 300];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = sreen3_2_exported
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end

Réponses (1)

Sourabh Kondapaka
Sourabh Kondapaka le 18 Fév 2021
Modifié(e) : Sourabh Kondapaka le 18 Fév 2021
Some of the changes you can do are:
  • Have a variable called 'detectedObj'. In your case CName0 represents the same thing. You need to assign the return value of classify() function as :
%By assigning it to a class variable, it will be accessible across all functions defined within the class.
app.detectedObj = classify(app.net,app.newing);
  • Because of the first point as stated above, in NutrientsCaloriesButtonPushed() function, you can now access the variable by app.detectedObj for the detected object. But you have to ensure that NutrientsColories Button is only clicked after SCANButton is clicked.
  • Read the excel sheet in the startUpFn of the app and store it in a variable. (This will save computation time when the excel sheet grows larger in size)
I would suggest you to go through the documentation to learn more about App Designer

Catégories

En savoir plus sur Develop Apps Using App Designer dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by