Use .mat file as input argument in a function.
조회 수: 18(최근 30일)
표시 이전 댓글
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?
답변(1개)
Siddharth Bhutiya
2022년 4월 5일
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
Tunde Adubi
2022년 4월 5일
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?
Stephen23
2022년 4월 6일
편집: Stephen23
2022년 4월 7일
"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
2022년 4월 6일
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?
Stephen23
2022년 4월 6일
편집: Stephen23
2022년 4월 7일
"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
2022년 4월 6일
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'.
Stephen23
2022년 4월 6일
"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
2022년 4월 6일
Hi, I am trying to call the function from the command line. Can you attempt it from your end? I attached the file. Thanks.
Stephen23
2022년 4월 7일
편집: Stephen23
2022년 4월 7일
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
Tunde Adubi
2022년 4월 7일
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.
Stephen23
2022년 4월 7일
편집: Stephen23
2022년 4월 7일
"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.
참고 항목
범주
Find more on Graphics Object Properties 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!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)