필터 지우기
필터 지우기

Why there is error in using interp2 while assigning loaded data to the variable?

조회 수: 1 (최근 30일)
I have a separate struct data file (.mat format) which I need to load and perform interp2.
The struct data file is as in the figure,
The matlab code runs fine while I am using following code
load("H2.mat");
h = interp2(data.P,data.T,data.H,235e+05,315);
But it gives error while using following code
data = load("H2.mat");
h = interp2(data.P,data.T,data.H,235e+05,315);
The error says
Unrecognized field name "P".
Error in hydrogen_enthalpy (line 5)
h = interp2(data.P,data.T,data.H,235e+05,315);
How can this problem be solved?
  댓글 수: 1
Stephen23
Stephen23 2023년 2월 28일
편집: Stephen23 2023년 2월 28일
"How can this problem be solved?"
By saving the data in the MAT file as separate arrays, and not stuck inside a scalar structure. This has other benefits too, e.g. the ability to load/replace individual arrays from the MAT file, or accessing them without loading using MATFILE.
Use the -STRUCT option to achieve this. For example:
D.hello = 'blah';
D.world = pi;
save ('test.mat','-struct','D')
Now all of the arrays are saved separately, not in one scalar structure:
whos -file test.mat
Name Size Bytes Class Attributes hello 1x4 8 char world 1x1 8 double
This means the data can be imported directly into the output structure as you expected:
S = load('test.mat')
S = struct with fields:
hello: 'blah' world: 3.1416
S.hello
ans = 'blah'
S.world
ans = 3.1416

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2023년 2월 17일
편집: Voss 2023년 2월 17일
Try this:
S = load("H2.mat");
data = S.data;
h = interp2(data.P,data.T,data.H,235e+05,315);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by