How can I use data from .mat file into spline function?

3 ビュー (過去 30 日間)
Hélio Filho
Hélio Filho 2021 年 6 月 17 日
編集済み: Stephen23 2021 年 6 月 18 日
I used the ginput function to select a lot of points, who were stored into a 'data.mat' file.
I now need to use the x and y coordinates into the spline function, which are saved into 'data.mat', but I don't know how to "call" the coordinates from the 'data.mat' file.
I already loaded the data:
S = load('dados.mat');
And I already have the plot ready, I just don't know how to "call" the x and y values.
x =
y =
xx = linspace(0,500,100);
yy = spline(x,y,xx);
figure;
plot(x, y, 'ro');
hold on;
plot(xx, yy, 'b', 'LineWidth', 1.5);
  5 件のコメント
SALAH ALRABEEI
SALAH ALRABEEI 2021 年 6 月 17 日
see the comment below by @Sulaymon, he just fix it.
Stephen23
Stephen23 2021 年 6 月 18 日
編集済み: Stephen23 2021 年 6 月 18 日
@Hélio Filho:
It is much more robust to load into an output variable (which is a scalar structure), just as you wrote in your question:
S = load(...)
and then access the fields of that structure, e.g.:
S.x
S.y
Either way you need to know and use the exact names of the data stored in the .mat file, which you can either get from the structure S (e.g. looking at it in the Variable Viewer or using fieldnames), or without even loading the filedata:
whos -file filename
Your original approach of loading into a structure is much better (it is less liable to bugs and easier to debug):

サインインしてコメントする。

採用された回答

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 6 月 17 日
Here is the corrected code:
...
save('data.mat', 'x','y'); % Your variable names called x, y and you need to save them here
load data.mat
end
xx = linspace(0,500,100);
yy = spline(x,y,xx);
figure;
plot(x, y, 'ro'); % x, y from ginput (data.mat)
hold on;
plot(xx, yy, 'b', 'LineWidth', 1.5);
  2 件のコメント
Hélio Filho
Hélio Filho 2021 年 6 月 17 日
編集済み: Hélio Filho 2021 年 6 月 17 日
I'm still getting the "Unrecognized function or variable 'x'." at the line where I first use x and y
  • yy = spline(x,y,xx);
Could it be I have an error in the data or something?
UPDATE: I did the acquiring the x and y data phase again, and I still get the same error, so it's not the file's problem.
UPDATE: So I fixed it, apparently there's such a thing as a "separator"? I don't know what it is, the text even changes color, and I dont know how I put it there, but basically i had the load before that separator, so it wasn't recognizing it. I pasted it below and I no longer have that error.
Even so, your answer helped me, so thanks!
Stephen23
Stephen23 2021 年 6 月 18 日
It is much more robust to load into an output variable (which is a scalar structure), just as the question shows:
S = load(...)
and then access the fields of that structure, e.g.:
S.x
S.y

サインインしてコメントする。

その他の回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 6 月 17 日
If all the data of x, y are saved under X, Y names from ginput in a sequenctial order in DATA.mat file.
You can load them and eploy, e.g.:
load DATA.mat
% x, y values will be in the MATLAB workspace
% Your code comes here
xx = linspace(0,500,100);
yy = spline(X,Y,xx);
...
  2 件のコメント
Hélio Filho
Hélio Filho 2021 年 6 月 17 日
Hi!
So what I have is:
while (t <= tf)
mov.CurrentTime=t; frame=readFrame(mov); image(frame); drawnow
tv(i)=t; t=t+dt; i=i+1; title(strcat('Frame ',num2str(i)));
[x(i) ,y(i)]=ginput(1);
end
save 'data.mat'
load data.mat
end
xx = linspace(0,500,100);
yy = spline(X,Y,xx);
figure;
plot(x, y, 'ro');
hold on;
plot(xx, yy, 'b', 'LineWidth', 1.5);
With this I get the 'Unrecognized function or variable 'X'' error. Since I called [x(i), y(i)] at the input function I should be able to call it, or am I wrong?
Stephen23
Stephen23 2021 年 6 月 18 日
It is much more robust to load into an output variable (which is a scalar structure), just as the question shows:
S = load(...)
and then access the fields of that structure, e.g.:
S.x
S.y

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by