How to plot multiple data in one graph/figure using plot function?

4 views (last 30 days)
Hi. I have made a program that let's the user enter a year between 2009 and 2011 and then show air temperature and snow depth for that year. I have two separate graphs showing up, one for snow depth and one for air temperature, but I want to show air temperature and snow depth in the same graph (figure). I also want to keep the figure window open after the user goes to the next part of the program. Is this possible using the plot function?
After this the user is asked to go to the next part of the program. Here, the user is asked if he wants to make the image in a shape of a square (equal lengths of sides). The problem is I only managed to stretch it out, I just want to cut/crop the image so all the sides have equal lengths.
valgtaar = input('Skriv inn aarstall: ');
snodybde = xlsread('sno2009_2011.xls')
snodybde2009 = snodybde(:,1)
snodybde2010 = snodybde(:,2)
snodybde2011 = snodybde(:,3)
lufttemperatur = xlsread('LT2009_2011.xls')
lufttemperatur2009 = lufttemperatur(:,1)
lufttemperatur2010 = lufttemperatur(:,2)
lufttemperatur2011 = lufttemperatur(:,3)
if valgtaar == 2009
figure(1);
bar(snodybde2009, 'b');
hold off
title(['Snodybde ' num2str(valgtaar)]);
xlabel('Dager/Maaned');
ylabel('Maalinger');
legend('snodybde i cm');
grid on
datetick('x','m');
figure(2);
bar(lufttemperatur2009, 'r');
hold off
title(['Lufttemperatur ' num2str(valgtaar)]);
xlabel('Dager/Maaned');
ylabel('Maalinger');
legend ('lufttemp i grader Celsius');
grid on
datetick('x','m');
y = menu('Gaa videre til del2', 'Ok','Avbryt');
elseif valgtaar == 2010
figure(1);
bar(snodybde2010, 'b');
hold off
title(['Snodybde ' num2str(valgtaar)]);
xlabel('Dager/Maaned');
ylabel('Maalinger');
legend('snodybde i cm');
grid on
datetick('x','m');
figure(2);
bar(lufttemperatur2010, 'r');
hold off
title(['Lufttemperatur ' num2str(valgtaar)]);
xlabel('Dager/Maaned');
ylabel('Maalinger');
legend ('lufttemp i grader Celsius');
grid on
datetick('x','m');
y = menu('Gaa videre til del 2', 'Ok','Avbryt');
elseif valgtaar == 2011
figure(1);
bar(snodybde2011, 'b');
hold off
title(['Snodybde ' num2str(valgtaar)]);
xlabel('Dager/Maaned');
ylabel('Maalinger');
legend('snodybde i cm');
grid on
datetick('x','m');
figure(2);
bar(lufttemperatur2011, 'r');
hold off
title(['Lufttemperatur ' num2str(valgtaar)]);
xlabel('Dager/Maaned');
ylabel('Maalinger');
legend ('lufttemp i grader Celsius');
grid on
datetick('x','m');
y = menu('Gaa videre til del 2', 'Ok','Avbryt');
else
disp('NB! Skriv inn 2009, 2010 eller 2011');
oblig3.m
end
if y == 1
clc;
close all;
imtool close all;
clear;
workspace;
format longg;
format compact;
fontSize = 20;
rgbImage = imread('michelle_pf.jpeg');
reductionFactor = 0.5;
[rows columns numberOfColorChannels] = size(rgbImage);
if rows > 400 || columns > 400
rgbImage = imresize(rgbImage, reductionFactor);
end
subplot(2,2,1);
imshow(rgbImage);
title('Original', 'FontSize', fontSize);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
m = menu('Skal bildet gjoeres kvadratisk?','Ja','Nei');
[rows columns numberOfColorChannels] = size(rgbImage);
squareWidth = min([rows columns]);
if m == 1
rgbImage = imresize(rgbImage, [squareWidth, squareWidth]);
subplot(2,2,2);
imshow(rgbImage);
title('Kvadratisk', 'FontSize', fontSize);
end
[rows columns numberOfColorChannels] = size(rgbImage);
[columnsInImage rowsInImage] = meshgrid(1:columns, 1:rows);
centerX = columns/2;
centerY = rows/2;
defaultValue = 45;
titleBar = 'Skriv inn et tall';
userPrompt = sprintf('Hva skal radius vaere for sirkel (mindre enn %d/2) ?', squareWidth);
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end;
radius = str2double(cell2mat(caUserInput));
if isnan(radius)
radius = defaultValue;
message = sprintf('Maa vaere et tall. Radius blir satt til halvparten av bildets minste dimensjon', radius);
radius = min([rows columns]) / 2;
uiwait(warndlg(message));
end
if radius > min([rows columns]) / 2
uiwait(msgbox('NB! Radius maa vaere mindre eller lik halvparten av bildets minste dimensjon. Radius blir satt til havlparten av bildets minste dimensjon'));
radius = min([rows columns]) / 2;
end
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
maskedRgbImage = bsxfun(@times, rgbImage, cast(circlePixels, class(rgbImage)));
subplot(2, 2, 3);
imshow(maskedRgbImage);
title('Maskert', 'FontSize', fontSize);
else
return;
end

Answers (1)

Andrew
Andrew on 21 Nov 2012
Dear Andrew, if I understand you right, you can plot several function in one graph using function "hold on", and if you want to keep your window with draft, you should delete line "close all" in the second part of your program. Best regards, Andrew

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!