how to plot multiple cdf plots in one figure?

i have a matrix in excel file in which i want to draw cdf for each column in a single figure. Help required asap. Or how to plot multiple vectors in one cdf plot?

Risposte (1)

Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
button = menu('Use which demo image?', 'CameraMan', 'Moon', 'Eight', 'Coins', 'Pout');
if button == 1
baseFileName = 'cameraman.tif';
elseif button == 2
baseFileName = 'moon.tif';
elseif button == 3
baseFileName = 'eight.tif';
elseif button == 4
baseFileName = 'coins.png';
else
baseFileName = 'pout.tif';
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(1, 2, 1);
imshow(grayImage, []);
caption = sprintf('%s (Original Grayscale Image)', baseFileName);
title(caption, 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Get the histogram of every 10th column and plot it.
subplot(1, 2, 2);
for column = 1 : 10 : columns
thisColumn = grayImage(:, column);
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(thisColumn);
cdf = cumsum(pixelCount) / numel(thisColumn);
plot(cdf, 'b-', 'Color', rand(1,3), 'LineWidth', 2);
if column == 1
hold on;
end
end
grid on;
title('CDF of columns', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.

27 Commenti

Thanks, sorry for being delayed in response. i got this error.,
"??? Error using ==> cdf at 49 Not enough input arguments"
Dear Sir, as you explained it far of my knowledge, i am beginner to matlab and just want to draw 3 CDF's of vectors cumulatively in single figure. Big Thanks
In my code, line 49 is this:
% Check if file exists.
and I ran it again and it ran fine, so that tells me that you modified my code and somehow made it not work. I can't tell how you broke it unless I see what you did.
If you just want the cdfs of 3 particular columns, then you can do that. Just extract 3 columns:
column1Profile = grayImage(:, column1);
column2Profile = grayImage(:, column2);
column3Profile = grayImage(:, column3);
then call hist and cumsum on each profile.
I m very grateful for your response, meanwhile i didn't modify the code just copied and pasted, and still it give that error. Moreover i have this excel file. In columns i have data rate of various ISP's for a period of time. I want to plot there CDF and represent each by different color, with color representing the name of ISP in legends. I know you wont have time for this silly and crap questions, but still i am hoping for your reply. Thanks in Advance.
Dear Sir, here is the image which i like to plot from this data set.
You must have called your m-file cdf, didn't you? That may cause the error. It's probably trying to call your m-file instead of using the variable by the same name. What does this show?
which -all cdf
Rename one of them, either the m-file or the variable.
It's done, u'r script is working now. I was making mistake with a file. i hope u see the image which i uploaded. I want to get an image like that from the excel file which i uploaded. Thanks
Yes, pretty much like mine except for only 4 curves. It should be trivial for you to adapt my code to take the cdf of only the 4 columns that you want.
can you give me the idea how to make this.. I am uploading the image and file again.. i think u missed it or i am not getting you. Thanks
waiting for u'r response
By figure, I thought you meant image. So just use xlsread and plot, something like (untested)
numbers = xlsread('cdf.slxs');
cdf1 = cumsum(numbers(:,1)) / sum(numbers(:,1));
cdf2 = cumsum(numbers(:,2)) / sum(numbers(:,2));
cdf3 = cumsum(numbers(:,3)) / sum(numbers(:,3));
plot(cdf1, 'b-', 'LineWidth', 2);
hold on;
plot(cdf2, 'r-', 'LineWidth', 2);
plot(cdf3, 'g-', 'LineWidth', 2);
Bundle of thanks for your reply, m really grateful to you.. But there is problem it doesn't give any figure,,, the Figure window is empty,, though no error's.
figure is empty showing the axis only,, the x-axis is also from 0 to 1 and y-axis as well
thanks in advance
Examine the variables in the debugger: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/ Are they correct?
numbers = xlsread('cdf.slxs');
cdf1 = cumsum(numbers(:,1)) / sum(numbers(:,1));
cdf2 = cumsum(numbers(:,2)) / sum(numbers(:,2));
cdf3 = cumsum(numbers(:,3)) / sum(numbers(:,3));
plot(cdf1, 'b-', 'Color', 'LineWidth', 2);
hold on;
plot(cdf2, 'r-', 'Color', 'LineWidth', 2);
plot(cdf3, 'g-', 'Color', 'LineWidth', 2);
??? Error using ==> cdfplot Too many input arguments.
Error in ==> Moon at 5 cdfplot(cdf1, 'b-','Color', 'LineWidth', 2);
What is cdfplot()? I thought you were using plot(). Is cdfplot the name of your m file? Please give the exact error message - don't snip or paraphrase.
the name of my m file is moon.m sorry sir, i edited the script u send, convert plot to cdf plot... now executed the same as u send and found that error
??? Error using ==> plot Invalid property found. Object Name : line Property Name : 'b-'.
Error in ==> Moon at 5 plot(cdf1, 'b-','Color', 'LineWidth', 2);
i am taking a lot of u'r time and thankful to you that u reply
Take out the 'Color' option.
Here is the output
i was expecting three lines :-(
Examine them in the debugger to see if they're all the same.
Still if they are three different lines, the x-axis shows the different values....
I think it could not be resolved in that way... But i am very thankful to you for your time,, though i didn't get what i want.. But your cooperation is highly appreciated. Big Thanks

Accedi per commentare.

Tag

Richiesto:

il 15 Feb 2014

Commentato:

il 18 Feb 2014

Community Treasure Hunt

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

Start Hunting!

Translated by