Plotting multiple graphs in one figure

2 vues (au cours des 30 derniers jours)
amy gravenstein
amy gravenstein le 25 Fév 2020
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 commentaires
Jalaj Gambhir
Jalaj Gambhir le 28 Fév 2020
Can you provide the values of all the variables like, X, Y, u...?
amy gravenstein
amy gravenstein le 2 Mar 2020
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!

Connectez-vous pour commenter.

Réponse acceptée

Jalaj Gambhir
Jalaj Gambhir le 2 Mar 2020
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 commentaire
amy gravenstein
amy gravenstein le 5 Mar 2020
Perfect! Thanks so much

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by