Why am I getting an error of subscripted assignment dimension mismatch

4 views (last 30 days)
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
Mohamad Khairul Ikhwan Zulkarnain
The thing that I supposed to do is:
I need to create a multidimensonal array of the element textfile to make it easier for the user the choose what element they want to read
From the element they chose, it need to create 201 new wavelength where the wavelength from the textfile which is in first column need to be in the center
From that, each of the wavelength need to go through the equation of the peak to calculate the peak and multiply it with int_fact where in the textfile(element in multidimensional array) is in the second column.
Lastly, all the peak created need to be added to create one spectral line and display it.
Thats how that code supposed to be. I learned it a bit from youtube and just throw it in to create this code. Sorry for the trouble. Hope you can help me with this. Thanks
Mohamad Khairul Ikhwan Zulkarnain
Do you think that from what i explained of what should i do the code is wrong? then how should I do it?

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 12 Sep 2018
Do you think that from what i explained of what should i do the code is wrong? then how should I do it?
The code you have written is probably wrong but as your explanation is not very clear I'm not sure. In particular, I don't really understand whether you're supposed to calculate something for all wavelengths, or just for the one the user select. I'm also not sure you've got your peak calculation right, your 2D numerator array is identical for all rows.
Here is an attempt at much cleaner code than you have written. As I'm not sure what it is exactly you want to do, the final result, the plot in particular, may not be correct.
%get list of element, prompt user to select one, and get content of element file
filelist = dir('*.txt');
[~, elementlist] = arrayfun(@(f) fileparts(f.name), filelist, 'UniformOutput', false);
[elementindex, ok] = listdlg('ListString', elementlist, 'PromptString', 'Select an element', 'SelectionMode', 'single');
if ~ok, return; end %terminate if user click cancel
elementdata = importdata(filelist(elementindex).name);
assert(isnumeric(elementdata), 'Element does not have data'); %abort if the file does not contain a matrix
%build wavelength array
step = 0.02;
ss = 100;
steparray = -step*ss : step : step*ss;
new_wavelength = elementdata(:, 1) + steparray; %no loop needed. Requires R2016b or latter
%get row selection
[selectedrow, ok] = listdlg('ListString', compose('%f', elementdata(:, 1)), 'PromptString', 'Select a wavelength', 'SelectionMode', 'single');
if ~ok, return; end %terminate if user click cancel
%build Peak vector for selected row
Delta_lambda = 0.2;
int_fact = elementdata(selectedrow, 2);
Peak = int_fact.*(1./(1+(steparray/Delta_lambda*2).^2));
plot(new_wavelength(selectedrow, :), Peak);
This is also a lot more user friendly than what you have written.
  8 Comments
Guillaume
Guillaume on 13 Sep 2018

So does that mean that when prompted to select a row, you want to be able to select a row? Your original code certainly did not work like that.

If that is the case, it's easy to change in part. First allow selection of multiple rows by changing the 'SelectionMode' to 'multiple':

%get row selection
[selectedrow, ok] = listdlg('ListString', compose('%f', elementdata(:, 1)), 'PromptString', 'Select wavelength(s)', 'SelectionMode', 'multiple');
if ~ok, return; end  %terminate if user click cancel

The rest of the code after that will work even if selectedrow is a vector (which will happen when you select multiple wavelengths).

As it is, the plot will have a line for each wavelength selected.

For your combined plot, you can't just sum the rows of Peak because the columns of each row correspond to different wavelengths. You first have to resample each row to the same wavelengths (with interp1 for example) before you can sum them. I don't have the time now to write that code for you.

Mohamad Khairul Ikhwan Zulkarnain
Okay then! you can write it later thats fine. I can try it first and see if it works. Thank you so much for the help! Much appreciated!

Sign in to comment.

More Answers (1)

Matt J
Matt J on 11 Sep 2018
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 Matrices and Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!