plot() doesn't work in .m file?

2 ビュー (過去 30 日間)
Niklas Kurz
Niklas Kurz 2020 年 6 月 20 日
回答済み: Devineni Aslesha 2020 年 6 月 23 日
I'm slightly flustered: I just fitted a graph and typed plot() beneath to plot another graph, but Matlab gives error:
%% Fit: 'untitled fit 1'.
[xData, yData] = prepareCurveData( N, A );
% Set up fittype and options.
ft = fittype( 'poly6' );
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
legend( h, 'A vs. N', 'untitled fit 1', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'N', 'Interpreter', 'none' );
ylabel( 'A', 'Interpreter', 'none' );
grid on
hold on
plot(x,y);
axis([-15 15 200 800])
Error in AmplitudeFit (line 36)
plot(x,y);
in command-window I can plot(x,y) without any issues.
  2 件のコメント
the cyclist
the cyclist 2020 年 6 月 20 日
So, plot() certainly works in m-files, so there is something weird about your case. It's difficult to diagnose without being able to replicate as much of what you are doing as possible. Can a self-contained piece of code that people could try to run and reproduce what you are seeing.
(It looks like the Curve Fitting Toolbox is required, so I won't be able to do that myself.)
Niklas Kurz
Niklas Kurz 2020 年 6 月 20 日
編集済み: Niklas Kurz 2020 年 6 月 20 日
alright I just forgot to include:
M = readtable('/Users/niklaskurz/Desktop/University/Documents/Exp.Physik/Organisation/Tk7 N/Eigenfrequenz mit I=0,2A');
x = M{:,1};
y = M{:,2};
I thought Matlab autimatically draws it from WorkSpace but the opposite was proven.

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

採用された回答

Devineni Aslesha
Devineni Aslesha 2020 年 6 月 23 日
Assuming that you have included the code for reading table into M and storing it in x and y before using plot(x,y), the error might be due to the character or string cell array resulted from the table. The plot function accepts numeric, categorical, datetime and duration datatypes. Refer this link to convert table variables containing characters or strings to categorical arrays.
Refer this link to plot categorical data.

その他の回答 (0 件)

製品

Community Treasure Hunt

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

Start Hunting!

Translated by