Use .mat file as input argument in a function.
Show older comments
Assuming a Data.mat file has a table of rows and columns, how can it be used as input arguement so that the elements in any row/column be called up for use in the function?
Answers (1)
Siddharth Bhutiya
on 5 Apr 2022
If you look at the value of K in your screenshot you will notice that it is a struct and one of the fields would be the table you are looking for (the name would be the name that you used while creating the MAT file). Once you figure out the name you can use the dot syntax to pass that as input to another function.
>> K = load("data.mat")
K =
struct with fields:
myTable: [4×4 table]
>> SVdata(K.myTable)
11 Comments
Tunde Adubi
on 5 Apr 2022
"but the the .mat file can't be used as input argument in a function using the dot syntax,..."
It is very common to provide structures and/or their fields as inputs to functions.
"...and that is where the problem lies."
What is much more likely is that you are doing something wrong. Lets have a look at your code (which you unfortunately uploaded as screenshots. In future please upload code as text).

K.T is not valid in a function signature like that, just like it is not possible to use any other indexing inside the function signature line. Do not do that.
On the 3rd and 4th lines of the function you wrote K.T, so clearly K should be the input argument, like this:
function [Xt,Yt,Zt] = SVdata(K)
% ^ indexing is not allowed here!
We can also see that although your script loads the data into structure K, you then do nothing with K and also do not call the local function. So your code ignores the imported data and does absolutely nothing with that function. Notice how MATLAB has underlined the function name (because it is unused): do not ignore these warnings.
Solution: you need to actually call the function with its required input arguments:
S = load(..);
[outX,outY,outZ] = SVdata(S)
There may be other bugs in your code, but because you did not upload text I could not check it.
Tunde Adubi
on 6 Apr 2022
"What can be done at the input for the function name to be used?"
In my last comment I explained that you need to call the function, and showed you how.
But you have not called the function.
Solution: call the function, just as I showed you in my last comment. Here it is again, I copied it here for you:
S = load("SatData.mat");
[outX,outY,outZ] = SVdata(S.T); % <- !!!!!!!! Call the function !!!!!!!!
Note that because you have now altered your function to accept the table, I provide only the table as the input argument (not the structure returned by LOAD).
Tunde Adubi
on 6 Apr 2022
Stephen23
on 6 Apr 2022
"I called the function just like you pointed, but it had the same error."
Then you have not defined the function correctly in the script. I cannot debug code that I cannot see.
Note that this
[OutX, OutY, OutZ]=SVdata('SatData.mat')
attempts to call the function from the command line. That will not work if the function is defined as a local function in a script (as you showed in this comment).
You need to decide:
- if you want to call the function from the command line then SVdata must be the main function (i.e. not written in a script nor as a local function).
- if you want to call the function in a script then it may be defined at the end of that script (exactly as you showed before. But then you will not be able to call it from the command line).
The first approach is easier, then you can call the function from anywhere.
It makes no sense to call the function with a character vector if you wrote it to accept a structure.
Tunde Adubi
on 6 Apr 2022
S = load('SatData.mat')
SVdata(S.T)
Note that because you have now altered your function to accept the table (rather than the structure), I provide only the table as the input argument (not the structure returned by LOAD). And because you have not defined Xt, Yt, and Zt within the function there are no output arguments when I called it (otherwise it will throw an error).
function [Xt, Yt, Zt] = SVdata(K) % as a main function, not in a script
t_oe = K(1,4);
e = K(1,2);
i = K(1,5); % Orbital Inclination(rad)
w = K(1,9); % Argument of Perigee(rad/s)
% Eccentricity Anomaly Iteration
E=0;
for z =1:100
%E = M +e*sin(E);
end
disp('everything works as expected')
end
Tunde Adubi
on 7 Apr 2022
"However, i am having unrecognized function error whenever i tied to call the function from command window. "
Because you have written a script, not a function file.
As I already explained in this comment, you cannot write a script and also call the local function from the command line. That simply will not work. Just as I wrote before, you need to decide:
- if you want to be able to call the function from the command line then you need to get rid of all code that is not within function...end . See: https://www.mathworks.com/help/matlab/ref/function.html
- if you really want a script (as you have now), then you cannot call the local function from the command line. See: https://www.mathworks.com/help/matlab/matlab_prog/local-functions-in-scripts.html
You have to decide what you want to do.
Tunde Adubi
on 7 Apr 2022
Categories
Find more on Live Scripts and Functions 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!