Calling table from matlab workspace as input for function
23 views (last 30 days)
Show older comments
Hi,
Disclaimer I'm relatively new to Matlab/coding in general so apologies if it's messy/unclear!
I am trying to create a function that takes a table in the workspace as an input for a function but receive an error. I have tried using 'FILENAME', "FILENAME", as well as trying to take the original excel file i.e. 'filename.xlsx' and a few other varieties but can't seem to get it right. I'm guessing it's because I need to define the file path but was wondering if there was a way to avoid that as I'm trying to help a few people in my research group by creating a generic bit of code that allows someone with practically no coding knowledge just to supply the four inputs as simply as possible (either directly from an excel file or from the table variable in matlab).
[m1,m2]=IRmax(FILENAME,1750,400,2)
function [mx1,mx2]=IRmax(FILENAME, UpperWn, LowerWn, NumMaxima)
%import data
data=readtable(FILENAME);
%labels variables
data.Properties.Variablenames{1}='wavenumber';
data.Properties.Variablenames{2}='absorbance';
%trim
trim=data(data.wavenumber>LowerWn & data.wavenumber<UpperWn,:);
%get local maxima
maxTS=table();
maxIR = (islocalmax(trim,"MaxNumExtrema",NumMaxima,"DataVariables","absorbance"));
%finds wavenumbers corresponding to maxima
maxTS.FILENAME=trim.wavenumber(maxIR(:,2)==1);
mx1=maxTS.FILENAME(1,1);
mx2=maxTS.FILENAME(2,1);
end
I receive these errors when running the code. I'm guessing that I'm going to encouter erros further down the line but I've been unable to get past this point
Error using readtable (line 318)
"filename" must be a string scalar or character vector.
Error in IRmax (line 4)
data=readtable(FILENAME);
Thanks in advance
0 Comments
Accepted Answer
Dave B
on 11 Nov 2021
when you call a function, you're passing in some values to be used in the function. So when you have:
[m1,m2]=IRmax(FILENAME,1750,400,2)
function [mx1,mx2]=IRmax(FILENAME, UpperWn, LowerWn, NumMaxima)
whatever is in the variable FILENAME in the outer workspace will get passed to FILENAME in IRmax's workspace. readtable expects that thing to be a string (or character vector) but it isn't. So what is in the variable FILENAME? Is that your table which you already read earlier? If so, you don't need to do readtable again, just pass that in as the function parameter data. If you're expecting it to be the name of the file, where did you define it above?
Here are some illustrative examples:
[m1,m2]=IRmax('mydata.xlsx',1750,400,2)
function [mx1,mx2]=IRmax(fn, UpperWn, LowerWn, NumMaxima)
%import data
data=readtable(fn);
%...
end
myfilename = 'mydata.xlsx';
[m1,m2]=IRmax(myfilename,1750,400,2)
function [mx1,mx2]=IRmax(fn, UpperWn, LowerWn, NumMaxima)
%import data
data=readtable(fn);
%...
end
mydata = readtable('mydata.xlsx');
[m1,m2]=IRmax(mydata,1750,400,2)
function [mx1,mx2]=IRmax(data, UpperWn, LowerWn, NumMaxima)
% data already imported, can just start working with it!
end
%...
end
More Answers (1)
Benjamin Pommer
on 12 Mar 2022
Hi,
I hava a related question. I have a function which shall have as an argument either variables from a table from the workspace or directly the .mat or .csv file and extract the necessary data from it. The function arguments shall be used for further calculation of what so ever.
The first time, I tried to get access to the variables velocity through the csv file, but it didnt work out.
function [A,X,res,x_vec,x_pred] = DMD_Kopie(filename,n,N_pred,states)
data = readtable(filename);
v = data.velocity(1:end)';
a = data.acc(1:end)';
t = data.t(1:end)';
E = data.energy(1:end)';
%further code
end
The first time, I tried to get access to the variables velocity through the corresponding .mat file. In that case, I got a struct array but again. It didnt work out.
function [A,X,res,x_vec,x_pred] = DMD_Kopie(filename,n,N_pred,states)
data = load(filename);
v = data.velocity(1:end)';
a = data.acc(1:end)';
t = data.t(1:end)';
E = data.energy(1:end)';
%further code
end
The last time, I loaded the variable into the workspace and tried to get access to them but again nothing worked out.
function [A,X,res,x_vec,x_pred] = DMD_Kopie(n,N_pred,states)
v = tablexyz.velocity(1:end)';
a = tablexyz.acc(1:end)';
t = tablexyz.t(1:end)';
E = tablexyz.energy(1:end)';
%further code
end
Do you have any idea how to solve this?
1 Comment
Dave B
on 12 Mar 2022
@Benjamin Pommer - I'd suggest you post this as a new question as that will help get lots of eyes on it. Also it's really helpful if you expand on 'didnt work out' because it's not clear what didn't work and how to help you. Folks will always want to know whether you got an error, and if so which one, and on which line of code. Or maybe you didn't get an error but the values weren't as expected.
Your first two code snippets look reasonable, aalthough the (1:end) part doesn't seem like it would do anything in this case.
I'd be curious whether there was something in the variables, is it that readtable didn't give you what you expected? Trying readtable outside of the function, or setting a breakpoint and stepping through the code are great ways to diagnose that sort of thing. How did you call the function, are you sure you correctly passed in 'filename'?
Your third code snippet wouldn't work because you didn't pass in the table, functions in general have a scope - they don't know about variables that weren't passed in as arguments.
See Also
Categories
Find more on Matrices and Arrays 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!