How do I get values from an excel sheet and calculate with them in appdesigner?
7 views (last 30 days)
Show older comments
Hi everyone,
I know this is not a new subject, but none of the existing questions fit my problem. I got an excel file with several sheets containing numeric data that I want to use for some calculations in appdesigner. The thing is I don't want to import the data as a fixed table, because the excel files will be extended in the future so I want to keep everything flexible.
Is it possible to get the data from my excel file without creating a fixed table? If yes, what does the corresponding code in appdesigner look like? How do I get the numeric data from a certain excel cell in a way that it's possible to calculate with it?
Thanks for your help!
Shanice
2 Comments
Sankarshan Durgaprasad
on 1 Dec 2021
You can choose to get a script from the import data on matlab.
Click on the bottom part of import selection and you'll see options to generate a script to import your data. :)
This script can then be added to your app designer.
Answers (1)
Ishaan Mehta
on 12 Feb 2024
Hi Shanice,
I understand that you want to import data from an Excel sheet for use in a MATLAB App Designer application.
I am assuming that importing current data from the sheet whenever the app is launched will satisfy the requirement you have mentioned. This way, the data will be refreshed every time the app is launched which would ensure that you are referencing to an up-to-date copy of the data in the sheet.
To import data from an Excel sheet programatically, you can use the "readtable" function in the "startupFcn" callback function in your application code. This will read the data from the excel sheet and store it in a property "dataTable" of the app when it is launched (make sure to add "dataTable" as a property in the "properties" block) . Below is a sample code snippet illustrating the same:
% Callback function: startupFcn
function startupFcn(app)
% Specify the path to the Excel file
excelFilePath = 'path_to_your_excel_file.xlsx';
% Check if the file exists
if isfile(excelFilePath)
% Read the data from the Excel file
inTable = readtable(excelFilePath);
% Assign the data to a property dataTable on the app object
app.dataTable = inTable;
else
% If the file does not exist, display an error message
uialert(app.UIFigure, 'Excel file not found.', 'File Error');
end
end
Make sure that the Excel file is either in the current working directory open in MATLAB, or is available on the MATLAB search path, or specify the full path to the Excel file.
Below are some links to MathWorks documentation pages for more reference regarding this approach:
- readtable: https://www.mathworks.com/help/matlab/ref/readtable.html
- startupFcn: https://www.mathworks.com/help/matlab/creating_guis/app-designer-startup-function.html
- class properties: https://www.mathworks.com/help/matlab/matlab_oop/defining-properties.html
Hope this helps!
0 Comments
See Also
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!