Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

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

1 Ansicht (letzte 30 Tage)
Mohamad Khairul Ikhwan Zulkarnain
Geschlossen: MATLAB Answer Bot am 20 Aug. 2021
% 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.

Antworten (1)

Bob Thompson
Bob Thompson am 11 Jul. 2018
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 Kommentare
Walter Roberson
Walter Roberson am 12 Jul. 2018
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) .';
Mohamad Khairul Ikhwan Zulkarnain
Thank you! Appreciated!

Diese Frage ist geschlossen.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by