フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

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

1 回表示 (過去 30 日間)
Mohamad Khairul Ikhwan Zulkarnain
Mohamad Khairul Ikhwan Zulkarnain 2018 年 7 月 11 日
閉鎖済み: 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
Mohamad Khairul Ikhwan Zulkarnain 2018 年 7 月 12 日
Thank you! Appreciated!

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by