Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

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

조회 수: 1 (최근 30일)
Mohamad Khairul Ikhwan Zulkarnain
마감: MATLAB Answer Bot 2021년 8월 20일
% 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.

답변 (1개)

Bob Thompson
Bob Thompson 2018년 7월 11일
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
Walter Roberson
Walter Roberson 2018년 7월 12일
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!

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by