How to read data for button in app designer using MATLAB (2024)

59 views (last 30 days)

Show older comments

Virajita Parameswaran on 16 Apr 2024 at 16:48

  • Link

    Direct link to this question

    https://in.mathworks.com/matlabcentral/answers/2107681-how-to-read-data-for-button-in-app-designer-using-matlab

  • Link

    Direct link to this question

    https://in.mathworks.com/matlabcentral/answers/2107681-how-to-read-data-for-button-in-app-designer-using-matlab

Commented: Virajita Parameswaran on 17 Apr 2024 at 8:17

Accepted Answer: Pratyush Swain

  • EB.mlapp

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 Comment

Show -1 older commentsHide -1 older comments

Rik on 17 Apr 2024 at 6:35

Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/2107681-how-to-read-data-for-button-in-app-designer-using-matlab#comment_3134141

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

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Pratyush Swain on 17 Apr 2024 at 8:07

  • Link

    Direct link to this answer

    https://in.mathworks.com/matlabcentral/answers/2107681-how-to-read-data-for-button-in-app-designer-using-matlab#answer_1442856

  • Link

    Direct link to this answer

    https://in.mathworks.com/matlabcentral/answers/2107681-how-to-read-data-for-button-in-app-designer-using-matlab#answer_1442856

  • EB.mlapp

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).

How to read data for button in app designer using MATLAB (4)

How to read data for button in app designer using MATLAB (5)

How to read data for button in app designer using MATLAB (6)

The excel data used to fill age group for party-1 is as follows:

How to read data for button in app designer using MATLAB (7)

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

1 Comment

Show -1 older commentsHide -1 older comments

Virajita Parameswaran on 17 Apr 2024 at 8:17

Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/2107681-how-to-read-data-for-button-in-app-designer-using-matlab#comment_3134261

  • Link

    Direct link to this comment

    https://in.mathworks.com/matlabcentral/answers/2107681-how-to-read-data-for-button-in-app-designer-using-matlab#comment_3134261

Thank u very much Pratyush Swain. Grateful for your detailed explanation.

Sign in to comment.

More Answers (0)

Sign in to answer this question.

See Also

Categories

Computational FinanceSpreadsheet LinkData Import from MATLAB

Find more on Data Import from MATLAB in Help Center and File Exchange

Tags

  • matlab
  • appdesigner

Products

  • MATLAB

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to read data for button in app designer using MATLAB (9)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

How to read data for button in app designer using MATLAB (2024)
Top Articles
Latest Posts
Article information

Author: Saturnina Altenwerth DVM

Last Updated:

Views: 6031

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Saturnina Altenwerth DVM

Birthday: 1992-08-21

Address: Apt. 237 662 Haag Mills, East Verenaport, MO 57071-5493

Phone: +331850833384

Job: District Real-Estate Architect

Hobby: Skateboarding, Taxidermy, Air sports, Painting, Knife making, Letterboxing, Inline skating

Introduction: My name is Saturnina Altenwerth DVM, I am a witty, perfect, combative, beautiful, determined, fancy, determined person who loves writing and wants to share my knowledge and understanding with you.