Plotting multiple graphs in one figure
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
amy gravenstein
le 25 Fév 2020
Commenté : amy gravenstein
le 5 Mar 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
Réponse acceptée
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!
Plus de réponses (0)
Voir également
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!