How can I find the values through the excel field that I imported to matlab?

5 views (last 30 days)
Hello all,
While I was doing my homework I have stucked at here. I have imported an excel file into matlab and the excel file consist of values such as Gender,Age,Weight,Smoking Status and Average Blood Pressure. I need to define these following two questions but I cant find out how to put it in a matlab function. Questions are,
1- What is the blood pressure of a 45-year-old, 75 kg male smoking cigarette?
2- What is the blood pressure of a 38-year-old non-smoker woman weighing 60 kg?
Any help is appreciated,

Answers (1)

Arjun
Arjun on 4 Feb 2025
Edited: Arjun on 4 Feb 2025
I understand that you want to read an excel file and then retrieve data from it based on some conditions.
In order to load data from the excel file, you can use 'readtable' function of MATLAB which creates a table by reading column-oriented data from a text file, spreadsheet (including Microsoft® Excel®) file, XML file, HTML file, or a Microsoft Word document. 'readtable' detects elements of your data, such as delimiter and data types, to determine how to import your data.
Once data is loaded, since you have more than one query you can define a function named 'findBloodPressure' which will return a double value 'bp' and will take 'data' which is the output of 'readtable', 'age', 'weight', 'gender', 'smokingStatus' as input arguments and will perform a logical indexing on the table 'data' by using the other input arguments for comparision and finally return 'bp'.
You can refer to the code below for implementation details:
% Import data from Excel file
data = readtable('your_file.xlsx');
% Define the function to perform logical indexing and return the result
function bp = findBloodPressure(data, age, weight, gender, smokingStatus)
% Use logical indexing to fetch data
filteredData = data(data.Age == age & ...
data.Weight == weight & ...
strcmp(data.Gender, gender) & ...
strcmp(data.SmokingStatus, smokingStatus), :);
if ~isempty(filteredData)
bp = filteredData.AverageBloodPressure;
else
% If no matching record found
bp = NaN;
end
end
bp1 = findBloodPressure(data, 45, 75, 'Male', 'Smoker');
bp2 = findBloodPressure(data, 38, 60, 'Female', 'Non-Smoker');
fprintf('The blood pressure of a 45-year-old, 75 kg male smoker is: %f\n', bp1);
fprintf('The blood pressure of a 38-year-old, 60 kg non-smoker woman is: %f\n', bp2);
Kindly refer the documentation of 'readtable' and 'strcmp' for more details:

Community Treasure Hunt

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

Start Hunting!