Why is my plot function does show any graph?

I have the following code which is shown below:
% creating a multidimensional array
files = dir('*txt') ; % you are in the folder with text files
N = length(files) ;
names = {files(:).name}' ;
iwant = cell(N,1) ;
for i = 1:N
iwant{i} = importdata(files(i).name) ;
end
% type iwant{i} where i refered to 1,2,3,....,n which refered to the number
% that has been assigned to each chemical element
A = input('Please enter an element in form of iwant{i}:');
% find the number of samples
n = length (A(:,1));
% step change for the wavelength
step = 0.02;
% wavelength which is at the centre
p = A(:,1);
% to make new wavelength for each of wavelength in the samples
for j=1:n
new_wavelength(j,:) = p(j)-step*10:step:p(j)+step*10;
end
format shortG
j = input('input number of row:')
Delta_lambda = 0.2
for k = 1:21;
numerator = new_wavelength(j,11)-new_wavelength(j,k)
denominator = Delta_lambda/2;
Denom = 1+(numerator/denominator)^2;
Intensity = 1/Denom
end
hold on
plot(new_wavelength(j,:),Intensity)
But i dont know why when i run it, it does not show any plot. Could anyone fix this up? I will include textfile as well

2 Comments

YOur code is a mess......what you re trying exactly?
I was trying to develop a graph by using the equation in the second loop by using the value develop from the first loop. Could you help me with this?

Sign in to comment.

 Accepted Answer

dpb
dpb on 13 Aug 2018
Edited: dpb on 13 Aug 2018
In
for k = 1:21;
numerator = new_wavelength(j,11)-new_wavelength(j,k)
denominator = Delta_lambda/2;
Denom = 1+(numerator/denominator)^2;
Intensity = 1/Denom
end
hold on
plot(new_wavelength(j,:),Intensity)
you overwrite Intensity every pass through the loop so there's only a single point at the end. There is a plot, it's probably just a single pixel so you don't notice there being a point.
The above could be rewritten as
denominator = Delta_lambda/2; % this is a constant
K=21; % the upper limit of the loop magic number
numerator = new_wavelength(j,11)-new_wavelength(j,1:K); % vectorize to have 21 elements
Intensity = 1./(1+(numerator/denominator).^2); % ditto for result
plot(new_wavelength(j,:),Intensity)

8 Comments

Thanks thats help! I just have one more question, that if I chose to run the equation for the second row from new_wavelength. How do I do if I want to run through all row from new wavelength unto the equation? Sorry Im quite new to this.Hope you can help me with this. Thanks!
" do if I want to run through all row from new wavelength "
Place another FOR loop over j around that section of code...then you have to decide what it is you want for result and write the logic to suit.
Do you want to plot multiple lines on single axes; a new figure for every line, a subplot for each, maybe?
Do you need to keep the result of the calculation for each element in the array? If so, then you'll have to allocate for the Intensity array and index to not overwrite it.
Or, factor the code some and turn this piece into another function that you can call. So many ways possible!!! :)
Hi, I want to plot multiple lines on a singles axes and if possible a figure for every line. I need to keep the result from the calculation as well. That one problem I dont really know how to do it. Can you help with this like showing how its done?
Im quite new to matlab so Im sorry for troubling you.
Actually, you can just use the vector operations of Matlab
den= Delta_lambda/2;
num= new_wavelength(:,11)-new_wavelength;
Intensity = 1./(1+(num/den).^2);
and you'll have the 2D array Intensity.
To plot multiple lines on a given axes, plot does that automagically by column so just rearrange from by row to by column:
plot(new_wavelength.',Intensity.')
To make a new figure for every one, loop over j in both arrays...
for j=1:size(new_wavelength,1)
figure
plot(new_wavelength(j,:),Intensity(j,:))
end
I tried the code that you gave me and it appear that it just plot the last row of data. Why is that happen?
Dunno, would have to see the actual code used...
What does
whos new_wavelength Intensity
return to verify what you actually calculated?
You also need to learn to use the debugger to examine your code so you can learn to find and fix logic errors on your own... :)

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects 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!