Plotting multiple graphs in one figure

1 回表示 (過去 30 日間)
amy gravenstein
amy gravenstein 2020 年 2 月 25 日
コメント済み: amy gravenstein 2020 年 3 月 5 日
Hello, I am new to matlab and have written a code to create a custom fit for data. I am hoping to plot multiple data sets with this fit in one figure. Currently, this code runs but it creates an individual plot for each data set, which is not what I want. Also, I am having difficulty with the legend formatting. I want to make each line a different color and label as y1,y2,y3... but what I have been currently doing is not working. Thank you.
%% create figure for each data set
w={}; %to collect coefficient values
y=Y(:,1);
hold all
for i=1:1:u %where u is the final column of y values
y=Y(:,i);
[xData, yData] = prepareCurveData( X, y );
ft = fittype( 'a*exp(-b*x)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.893783889527977 0.298767974365371];
% Plot fit with data.
figure( 'Name', ['y',sprintf('%d',i)] );
% Fit model to data.
[fitresult{i}, gof(i)] = fit( xData, yData, ft, opts );
disp(fitresult)
hold all % from here downwards is where I find difficulty plotting correctly
for j=1:1:u
h = plot( fitresult{i}, xData, yData);
legendInfo{i}=[num2str(i),'=y'];
legend(legendInfo);
v =coeffvalues(fitresult{i});
w =[w v];
end
hold off
% Label axes
xlabel X
ylabel y_i
grid on
end
hold off
  2 件のコメント
Jalaj Gambhir
Jalaj Gambhir 2020 年 2 月 28 日
Can you provide the values of all the variables like, X, Y, u...?
amy gravenstein
amy gravenstein 2020 年 3 月 2 日
I start with a 4 by 14 matrix. X is the first column of the matrix. Y is the remaining 4 by 13 matrix. u is the number of columns in Y which is 13. w and v are used to gather the coefficient values. If there is anything more you need please let me know. Thanks!

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

採用された回答

Jalaj Gambhir
Jalaj Gambhir 2020 年 3 月 2 日
Hi,
The problem is specifying creation of a figure inside the outer for loop. You can declare it outside, specifying a uniform title (because you want a single figure), and removing the first 'hold off' command (just outside the inner for loop)
Hope this helps!
  1 件のコメント
amy gravenstein
amy gravenstein 2020 年 3 月 5 日
Perfect! Thanks so much

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by