Why I cant make a loop from the textfile I chose?

% 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
% select which element is needed
A = input('Please enter an element:');
% 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 i=1:n
new_wavelength(n) = p-step*10:step:p+step*10
end
I want to make a loop from all the row and first column from the element I chose but it gave me the respond below:
Please enter an element:iwant{2}(:,1) % this is the input i wrote
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in saja (line 19)
new_wavelength(n) = p-step*10:step:p+step*10
Where did I go wrong? Can someone figure it out? I will attached the folder that i have put in array.

Answers (1)

new_wavelength(n) = p-step*10:step:p+step*10
new_wavelength(n) is only a single element, because n is a single value. new_wavelength is an array of size 1xn, but your indexing is calling only a single element of it.
p is an array of size nx1 (p is actually just a copy of the first column of A), and so you cannot fit an entire array into a single element of a double array. That's why you're getting the error.

5 Comments

So what is the right command for me then? I want for each value in the first column to have 20 more new wavelength. How do I write command for that?
Do you want the second column to be the first column plus 20, and the third column to be the second column plus 20, and so on, with column #K being the first column plus (K-1)*20 ?
I want to create a range of 20 new data for each data in the first column and second column. Thats why I try to use the command below:
for i=1:n
new_wavelength(i) = p(:,1)-step*10:step:p(:,1)+step*10
end
But it doesnt work out. Could you help me with it?
p = A(:,1);
offset = step * (-10:10);
new_wavelength = bsxfun(@plus, p, offset);
This will create a matrix in which each row corresponds to a different entry in A(:,1). There will be 21 entries for each row (not 20 entries: remember the 0 relative offset in the middle.). If you want columns instead of rows, then use
new_wavelength = bsxfun(@plus, p, offset) .';

This question is closed.

Tags

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!