MATLAB Answers

Function and xls doesn't work?

3 ビュー (過去 30 日間)
Gianna Gokeler
Gianna Gokeler 2021 年 3 月 16 日
回答済み: Maadhav Akula 2021 年 3 月 22 日
Hello my function won't run when I generate a function plus my script calling the function also wont run?
Can someone please help me
function Hamstrings = importfileFunc1(workbookFile,sheetName,dataLines)
%IMPORTFILE1 Import data from a spreadsheet
% HAMSTRINGS = IMPORTFILE1(FILE) reads data from the first worksheet in
% the Microsoft Excel spreadsheet file named FILE. Returns the numeric
% data.
%
% HAMSTRINGS = IMPORTFILE1(FILE, SHEET) reads from the specified
% worksheet.
%
% HAMSTRINGS = IMPORTFILE1(FILE, SHEET, DATALINES) reads from the
% specified worksheet for the specified row interval(s). Specify
% DATALINES as a positive scalar integer or a N-by-2 array of positive
% scalar integers for dis-contiguous row intervals.
%
% Example:
% Hamstrings = importfile1("/Users/giannagokeler/pCloud Drive/Hamstrings.xlsx", "pone.0241127.s001", [2, 144]);
%
% See also READTABLE.
%
% Auto-generated by MATLAB on 11-Mar-2021 13:07:39
%% Input handling
% If no sheet is specified, read first sheet
if nargin == 1 || isempty(sheetName)
sheetName = 1;
end
% % If row start and end points are not specified, define defaults
if nargin <= 2
dataLines = [2, 144];
end
%% Setup the Import Options
opts = spreadsheetImportOptions("NumVariables", 49);
% Specify sheet and range
opts.Sheet = sheetName;
opts.DataRange = "A" + dataLines(1, 1) + ":AW" + dataLines(1, 2);
% Specify column names and types
opts.VariableNames = ["player_id", "age", "body_mass", "bmi", "body_height", "dominant_leg", "play_position", "sit_reach", "nordic_hamstring_test", "hamstrings_concentric_60_dom", "hamstrings_concentric_60_nondom", "quadriceps_concentric_60_dom", "quadriceps_concentric_60_nondom", "hamstrings_quadriceps_ratio__60_dom", "hamstrings_quadriceps_ratio_60_nondom", "hamstrings_concentric_240_dom", "hamsrings_concentric_240_nondom", "quadriceps_concentric_240_dom", "quadriceps_concentric_240_nondom", "hamstring_quadriceps_ratio_240_dom", "hamstring_quadriceps_ratio_240_nondom", "hamstring_eccentric_30_dom", "hamstring_eccentric_30_nondom", "hamstring_eccentric_120_dom", "hamstring_eccentric_120_nondom", "CMJ_height", "Illinois_agility_test", "dash_20m", "dash_40m", "previous_hamstring_injury", "total_hamstring_injuries", "Time_exposure_hours_Training", "Time_exposure_hours_Match", "Time_exposure_hours_Training_and_Match", "Injury_Rate_training", "Injury_Rate_match", "Injury_Rate_TOTAL", "Relative_Hamstring_torque_60_dom", "Relative_Hamstring_torque_60_nondom", "Relative_Quadriceps_torque_60_dom", "Relative_Quadriceps_torque_60_nondom", "Relative_Hamstring_torque_240_dom", "Relative_Hamstring_torque_240_nondom", "Relative_Quadriceps_torque_240_dom", "Relative_Quadriceps_torque_240_nondom", "Relative_Hamstring_torque_eccentric_30_dom", "Relative_Hamstring_torque_eccentric_30_nondom", "Relative_Hamstring_torque_eccentric_120_dom", "Relative_Hamstring_torque_eccentric_120_nondom"];
opts.SelectedVariableNames = ["player_id", "age", "body_mass", "bmi", "body_height", "dominant_leg", "play_position", "sit_reach", "nordic_hamstring_test", "hamstrings_concentric_60_dom", "hamstrings_concentric_60_nondom", "quadriceps_concentric_60_dom", "quadriceps_concentric_60_nondom", "hamstrings_quadriceps_ratio__60_dom", "hamstrings_quadriceps_ratio_60_nondom", "hamstrings_concentric_240_dom", "hamsrings_concentric_240_nondom", "quadriceps_concentric_240_dom", "quadriceps_concentric_240_nondom", "hamstring_quadriceps_ratio_240_dom", "hamstring_quadriceps_ratio_240_nondom", "hamstring_eccentric_30_dom", "hamstring_eccentric_30_nondom", "hamstring_eccentric_120_dom", "hamstring_eccentric_120_nondom", "CMJ_height", "Illinois_agility_test", "dash_20m", "dash_40m", "previous_hamstring_injury", "total_hamstring_injuries", "Time_exposure_hours_Training", "Time_exposure_hours_Match", "Time_exposure_hours_Training_and_Match", "Injury_Rate_training", "Injury_Rate_match", "Injury_Rate_TOTAL", "Relative_Hamstring_torque_60_dom", "Relative_Hamstring_torque_60_nondom", "Relative_Quadriceps_torque_60_dom", "Relative_Quadriceps_torque_60_nondom", "Relative_Hamstring_torque_240_dom", "Relative_Hamstring_torque_240_nondom", "Relative_Quadriceps_torque_240_dom", "Relative_Quadriceps_torque_240_nondom", "Relative_Hamstring_torque_eccentric_30_dom", "Relative_Hamstring_torque_eccentric_30_nondom", "Relative_Hamstring_torque_eccentric_120_dom", "Relative_Hamstring_torque_eccentric_120_nondom"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double"];
% Import the data
Hamstrings = readtable(workbookFile, opts, "UseExcel", false);
for idx = 2:size(dataLines, 1)
opts.DataRange = "A" + dataLines(idx, 1) + ":AW" + dataLines(idx, 2);
tb = readtable(workbookFile, opts, "UseExcel", false);
Hamstrings = [Hamstrings; tb]; %#ok<AGROW>
end
%% Convert to output type
Hamstrings = table2array(Hamstrings);
end
%% Title:
%
%% Example:
% Author: Gianna Gokeler
% Copyright 2021
%% clear and close
clear;
close all;
%% setup
% importdata('Hamstings.xlsx')
Hamstrings = importfileFunc1(workbookFile, sheetName, dataLines)

回答 (1 件)

Maadhav Akula
Maadhav Akula 2021 年 3 月 22 日
Hi,
I presume you have generated this function by choosing the Generate Function under Import Selection, and then saving the function appropriately.
Now to call the function you can execute the following statement in command window or a script as follows:
Hamstrings = importfileFunc1('Hamstrings.xlsx', 1, [2, 144]);% Setting the arguments to default values, you can modify accordingly.
Also please ensure that the relevant file(in our case 'Hamstrings.xlsx') and the generated function is on MATLAB path while you execute the above statement.
Hope this helps!

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by