How do I get values from an excel sheet and calculate with them in appdesigner?

5 次查看(过去 30 天)
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 个评论
Sankarshan Durgaprasad
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.
Shanice Steinecke
Shanice Steinecke 2021-12-2
Thank you so much!
Could you please show me what the code exactly looks like when I want to add this script to app designer? (This is a new topic and I don't know how to start...)
And how do I address a specific value from this script to use it for my calculations?
Thanks in advance!

请先登录,再进行评论。

回答(1 个)

Ishaan Mehta
Ishaan Mehta 2024-2-12
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:
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by