How to read data for button in app designer using MATLAB

10 visualizzazioni (ultimi 30 giorni)
I have created an app, I'm able to generate 'N' number of tabs for the given input. I'm able to add editfields, dynamic input boxes and buttons to those tabs. My question is after entering numeric values in the dynamic input boxes and clicking on enter i want to generate again those many dynamic input boxes for each input numeric value. Example:If i give number of election boxes as '5', i'm able to generate that many tabs. In those tabs i added election box no., number of partys, and different age group people for each party. I'm able to generate dynamic input boxes also. In different age group people for each party i have generated dynamic boxes. In those boxes if i enter some value '3' and '2' in each input box and click enter i should generate again those many input boxes with respective to the value of each input box.
  1 Commento
Rik
Rik il 17 Apr 2024

You forgot to ask a question. Have a read here and here. It will greatly improve your chances of getting an answer.

Accedi per commentare.

Risposta accettata

Pratyush Swain
Pratyush Swain il 17 Apr 2024
Hi Vishuka,
After going through your shared code, I added an extra property named DynamicInputBoxes_2 in order to store the values of age groups for each of the party. It is an 2D cell array because each party in a tab will contain its own set of age groups. I also added a button as for receiving the number of different age groups for different parties and accordingly added a callback function as follows:
function enterButtonPushed_3(app, tab, ElectionBox)
% Creating UILabel %
uilabel(tab, ...
'Text', 'Enter age grps for each Party:', ...
'Position', [40, 325, 350, 22]);
% Read Num of parties for current tab%
numParties = app.editFields(ElectionBox).Value;
% Create a 2D cell array to store age group for each party %
app.DynamicInputBoxes_2{ElectionBox} = cell(1,numParties);
initialPosition = [50, 320 , 50, 22];
% Iterate for each of the party %
for j = 1:numParties
initialPosition = initialPosition + [(j-1)*70,0,0,0];
numAgeGrps = app.DynamicInputBoxes_1{ElectionBox}(j).Value;
app.DynamicInputBoxes_2{ElectionBox,j} = gobjects(1,numAgeGrps);
% Dynamically create input boxes for each age group for each party %
for k = 1:numAgeGrps
fieldPosition = initialPosition +[0,-(k)*30, 0, 0];
app.DynamicInputBoxes_2{ElectionBox,j}(k) = uieditfield(tab,'numeric','Position', fieldPosition);
end
% In the end include a import from excel button to read age
% group data from excel file
if numAgeGrps > 0
uibutton(tab, 'push', ...
'Position', initialPosition + [0,-(numAgeGrps+1)*30,0,0], ...
'Text', 'Imp.Excel', ...
'ButtonPushedFcn', @(btn,event) fillDataFromExcel(app, ElectionBox, j));
end
end
end
In your original query, you also had mentioned regarding importing values for age groups. Assuming you wanted to import age group values for each party separately , I created a fillDataFromExcel as follows:
function fillDataFromExcel(app, ElectionBox, partyId)
% Prompt User to select Excel File%
[file, path] = uigetfile({'*.xlsx';'*.xls'}, 'Select an Excel file');
if isequal(file,0) || isequal(path,0)
warning('Invalid File');
return;
else
excelFilePath = fullfile(path, file);
end
%Assuming a single column data in the excel file (As this
% function can be invoked for each party) %
data = readmatrix(excelFilePath, 'Range', 'A:A');
numAgeGrps = app.DynamicInputBoxes_1{ElectionBox}(partyId).Value;
% Checking data points of input boxes vs Excel column %
if numAgeGrps ~= numel(data)
warning('The number of data points in the Excel file does not match the number of input boxes.');
end
% Filling in the Data %
for k = 1:min(numAgeGrps, numel(data))
app.DynamicInputBoxes_2{ElectionBox, partyId}(k).Value = data(k);
end
end
Finally below are some outputs of the app working suiting your use case - (dynamic input box creation & import from excel).
The excel data used to fill age group for party-1 is as follows:
I have attached the source code with this post. I hope this modifications will be helpful for your usecase. Please note this is only a proof of concept prototype and it may need further UI modification or any missing/edge case analysis.
For more information on appdesigner,please refer to https://www.mathworks.com/help/matlab/app-designer.html

Più risposte (0)

Categorie

Scopri di più su Develop Apps Using App Designer in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by