Why is my plot function does show any graph?

3 ビュー (過去 30 日間)
Mohamad Khairul Ikhwan Zulkarnain
Mohamad Khairul Ikhwan Zulkarnain 2018 年 8 月 13 日
コメント済み: dpb 2018 年 8 月 13 日
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 件のコメント
KSSV
KSSV 2018 年 8 月 13 日
YOur code is a mess......what you re trying exactly?
Mohamad Khairul Ikhwan Zulkarnain
Mohamad Khairul Ikhwan Zulkarnain 2018 年 8 月 13 日
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?

サインインしてコメントする。

採用された回答

dpb
dpb 2018 年 8 月 13 日
編集済み: dpb 2018 年 8 月 13 日
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 件のコメント
Mohamad Khairul Ikhwan Zulkarnain
Mohamad Khairul Ikhwan Zulkarnain 2018 年 8 月 13 日
I tried the code that you gave me and it appear that it just plot the last row of data. Why is that happen?
dpb
dpb 2018 年 8 月 13 日
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... :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by