Use .mat file as input argument in a function.

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)

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

Thank you.
I see that K loads a .mat file with struct fields T, but the the .mat file can't be used as input argument in a function using the dot syntax, and that is where the problem lies. I want to be able to use the rows/columns values in the table for my equations in the function. Is there any other means?
"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.
Hi, The function name remains unused even after calling the structure k as the input argument.
Here is an excerpt of the script. function_name is being underlined, and I'm using K as matrix/table that's why I indexed to select specific data.
K=load("SatData.mat");
function [Xt, Yt, Zt] = SVdata(K)
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
end
What can be done at the input for the function name to be used?
"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).
I called the function just like you pointed, but it had the same error.
S=load('SatData.mat');
[OutX, OutY, OutZ] = SVdata(S);
>> data
Unrecognized function or variable 'SVdata'.
Error in data (line 2)
[OutX, OutY, OutZ] = SVdata(S);
>> [OutX, OutY, OutZ]=SVdata('SatData.mat')
Unrecognized function or variable 'SVdata'.
"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.
Hi, I am trying to call the function from the command line. Can you attempt it from your end? I attached the file. Thanks.
S = load('SatData.mat')
S = struct with fields:
T: [31×13 table]
SVdata(S.T)
everything works as expected
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
S=load('SatData.mat');
SVdata(S.T)
function [Xt, Yt, Zt] = SVdata(K)
% Constants
u = 3.986005e14; % earth gravitational constant (m^3/s^2)
Omega_e = 7.2921151467e-5; % earth's rotation rate (rad/sec)
% Almanac Parameters
e = K(1,2); % eccentricity:
a = K(1,7)^2; %semi-major Axis
t_oe = K(1,4); % epoch orbital time of applicability
i= K(1,5); %Orbital Inclination(rad):
w = K(1,9) ; %Argument of Perigee(rad):
Mo = K(1,10); %Mean Anomaly at (rad):
Omega_n= K(1,8); %Right Ascen at Week(rad):
Omega_dot = K(1,8); %Rate of Right Ascen(r/s):
t=t_oe + (1/2)*3600; % GPS time
t_k=t-t_oe; % Time from eph ref epoch (s)
n=sqrt(u/a^3); %mean motion
M=Mo+n*t_k; % Mean anomaly (rad/s)
% Eccentricity Anomaly Iteration
E=0;
for z =1:100
E = M +e*sin(E);
end
E = M+e*sin(E); % Eccentric Anomaly
r = a*(1-(e*cos(E))); %orbit radius
v = atan2(sqrt(1-e^2)*sin(E), cos(E)-e); % true Anomaly
% SV coordinates in rotated orbit
xp=r*cos(w+v);
yp=r*sin(w+v);
Omega_p= Omega_n + (Omega_dot - Omega_e)*(t_k)-(Omega_e)*t_oe;
%This converts SV position to ECEF coordinates
Xt =xp*cos(Omega_p) - yp*sin(Omega_p)*cos(i);
Yt=xp*sin(Omega_p) - yp*cos(Omega_p)*sin(i);
Zt =yp*sin(i);
fprintf('%9s %22.11f\n','Omega_p',Omega_p);
fprintf("The Satellite ECEF coordinates for PRN1 are:\n")
fprintf('\n%9s %22.11f\n','Xt',Xt);
fprintf('%9s %22.11f\n','Yt',Yt);
fprintf('%9s %22.11f\n','Zt',Zt);
end
Yeah,Thanks. It looks good. However, i am having unrecognized function error whenever i tied to call the function from command window.
"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:
You have to decide what you want to do.
Oh, sure. I will go through those links and follow it up. Thank you.

Sign in to comment.

Categories

Find more on Live Scripts and Functions in Help Center and File Exchange

Asked:

on 3 Apr 2022

Commented:

on 7 Apr 2022

Community Treasure Hunt

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

Start Hunting!