Clear Filters
Clear Filters

generate Excel read boxes

3 views (last 30 days)
I have created an app, where i have to generate excel read boxes instead of dynamic input boxes. Example: if i give number of boxes as 5, i'm able to generate 5 tabs, and able to add editfields and enter button to the app. In editfield named as number of bags, if i enter 5 i'm able to generate 5 input boxes. In each input boxes i'm after entering the number of balls in each bag and clicking the enter button. Now i want to generate those many excel read boxes( If i enter 2 in one of the dynamic input box and click enter i want to generate 2 excel read file boxes, where each excel read file box is for each ball ), I have attached the file for further references.

Accepted Answer

Pratyush Swain
Pratyush Swain on 18 Apr 2024
Hi Virajita,
Since your shared app designer code file already contains implementation for generating dynamic input boxes, I am assuming reading excel files and alloting values to the input boxes is your prime roadblock.
Please refer to uiget function to select excel file through GUI and then you can utilise the readcell or readmatrix function to extract data from the excel file. You can refer to these MATLAB answer threads for the same:
If you still face difficulty in implementation , you can refer to a very identical question thread which has been answered in detail: https://www.mathworks.com/matlabcentral/answers/2107681-how-to-read-data-for-button-in-app-designer-using-matlab
Hope this helps.
  7 Comments
Pratyush Swain
Pratyush Swain on 19 Apr 2024
Hi Virajita,
Please go through this example workflow, which will help you in your usecase:
1- I have added a new property to your app named "readExcelButtons" which will store the graphics object for "read excel" buttons corresponding to box number, ball set number & ball number. I modiified the earlier property "ImportCelldata" to only capture the data from each read excel buttons.
2- I have modified the "enterButtonPushed_2" function accordingly:
function enterButtonPushed_2(app, tab,Box)
numBalls = app.editFields(Box).Value;
app.ImportCelldata{Box} = cell(1,numBalls);
app.ExcelReadButtons{Box} = cell(1,numBalls);
initialPosition = [50, 375 , 50, 22];
for n = 1:numBalls
initialPosition_1 = initialPosition + [(n-1)*70,0,0,0];
numCells = app.DynamicInputBoxes{Box}(n).Value;
app.ImportCelldata{Box,n} = cell(1,numCells);
app.ExcelReadButtons{Box,n} = gobjects(1,numCells);
for i = 1:numCells
fieldPosition_1 = initialPosition_1 +[0,-(i)*30, 0, 0];
app.ExcelReadButtons{Box,n}(i)=uibutton(tab, 'push', ...
'Position', fieldPosition_1, ...
'Text', 'Imp.Excel', ...
'ButtonPushedFcn', @(btn,event) readExcelButtonPushed(app, btn, Box, n ,i));
end
end
end
3- Also I added a new funtion "readexcelButtonPushed" as a callback to readexcel buttons:
% Addded comments for better understanding %
function readExcelButtonPushed(app, btn, Box, Ball, Cell)
[file, path] = uigetfile({'*.xlsx;*.xls', 'Excel Files (*.xlsx, *.xls)'}, 'Select an Excel File');
if isequal(file, 0)
warning('No valid excel file found');
else
fullPath = fullfile(path, file);
% Read the file into a matrix
data = readmatrix(fullPath);
% Store the data in your property variable
app.ImportCelldata{Box, Ball , Cell} = data;
% Change the button color to green and text to operation done to indicate success
btn.BackgroundColor = [0.0, 1.0, 0.0];
btn.Text = "Op.Done";
end
end
Please note here the "app.ImportCelldata{Box, Ball , Cell}" will contain your relevant data and you can use it later to plot data. Also please modify the data reading part from excel as per your expected data.
After the changes the app takes the following shape:
Please note here after clicking a "import excel button" and selecting file,it changes to green with text as "Operation done" signifying read operation has been performed. We can fetch the values from the "ImportCelldata" property as follows:
The app.ImportCelldata{1,2,1} points to the excel data assigned in the 1st Box, 2nd Ball set,1st Ball.Similarly you can see app.ImportCelldata{1,2,2} currently contains no data.
Hope this example workflow helps you , I have attached the file for your reference, you can modify it as per your need.
Thanks.
Vahini Polishetty
Vahini Polishetty on 24 Apr 2024
Hi @Pratyush Swain, I encounter with an issue from above code. The issue is while we are able to generate dynamic excel read buttons from above code. If we change the input at number of balls for each bag, the dynamic excel read buttons are not changing according to the input given. To overcome this issue i have added below code. But it is only able to change the first excel read button and unable to change for other inputs.
I have added this code after, function enterButtonPushed_2(app, tab,Box)
code:
if ~isempty(app.ExcelReadButtons{Box})
delete(app.ExcelReadButtons{Box});
end

Sign in to comment.

More Answers (0)

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!