Why am I getting an error of subscripted assignment dimension mismatch
Show older comments
Can anyone tell me how to fix the problem?
% 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
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;
ss=100;
zn=ss+1;
% 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*ss:step:p(j)+step*ss;
end
format shortG
jj = input('Please input the row:');
jj = jj.';
i = input('Please input number for element:');
Delta_lambda=0.2;
int_fact= iwant{i}(jj,2);
K=2*ss+1;
denominator = Delta_lambda/2;
for m = 1:length(new_wavelength(:,1))
for K = 1:length(new_wavelength(1,:)) % the upper limit of the loop magic number
numerator(m,K) = new_wavelength(m,zn)-new_wavelength(m,K);
end
Peak(m,K) = int_fact.*(1./(1+(numerator(m,K)/denominator).^2));
hold on
end
plot(new_wavelength(j,:).',Peak(m,K).','g')
hold on
I will attached the file as well Thanks
9 Comments
Matt J
on 11 Sep 2018
What line is throwing the error? And what is on the right hand side of that line? A scalar, or something else?
Mohamad Khairul Ikhwan Zulkarnain
on 11 Sep 2018
Guillaume
on 11 Sep 2018
With which element, value of jj and value of i, do you get an error? I've just tried with aluminium, jj = 4, i = 2, and got no error.
Note that there are a lot of things that don't make sense in your code, in particular there are a few transpose (.') on scalars (including in your plot call),
Mohamad Khairul Ikhwan Zulkarnain
on 11 Sep 2018
Just tried with copper (iwant{25}), row 4, element 2 and got no error.
What I mean is that you have:
jj = jj.';
yet jj is scalar. So the above line won't do anything.
Similarly, in your plot you have Peak(m,K).'. Again, the transpose won't do anything since peak(m,K) is scalar.
Also, you create a names cell array that you never use.
You also have
format shortG
which won't do anything. format only affects the way numbers are displayed on the command line. Nothing else.
Finally, you have hold on in your loop, yet never plot anything in the loop.
Note that Peak is a matrix, not a vector. Peak(m,K) is of course scalar.
edit: Another, more critical problem with your code is you never preallocate or reset any of the matrices that you fill so if you call your script several time, you'll end up using values for a different element. While it is a bug it shouldn't cause the error you state.
Mohamad Khairul Ikhwan Zulkarnain
on 11 Sep 2018
Guillaume
on 11 Sep 2018
"i need Peak(m,K) to be a matrix as well"
That makes no sense. If Peak is a matrix, then a single element of that matrix, Peak(m, K) is scalar.
Looking in more detail at your code, there are more things that don't make sense. Is the element you chose for your question Please input number for element supposed to be te same as for your question Please enter an element in form of iwant{i}. If so, why do you ask for it twice?
Then, shouldn't the calculation of Peak(m, K) inside the K loop. As it is, you're calculating a lot of numerator(m, K) that you don't use and only fill the last column of Peak.
To be honest, it would be easier if you explained what your code is supposed to do and we rewrite it all.
Mohamad Khairul Ikhwan Zulkarnain
on 11 Sep 2018
Mohamad Khairul Ikhwan Zulkarnain
on 12 Sep 2018
Accepted Answer
More Answers (1)
Matt J
on 11 Sep 2018
0 votes
The Peak(m,K) line (line 35). It is a vector
Well, then I think you've answered your own question. You cannot stick a vector into a scalar location Peak(m,K).
Categories
Find more on Characters and Strings 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!