how to create a for loop 380:780?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hi,
how can I create a for loop between in this statement
CM4_380 = spec_data(spec_data.wavelength>380 & spec_data.wavelength<381,:);
so it for loops between 380:780? Basically, to make this:
CM4_381 = spec_data(spec_data.wavelength>381 & spec_data.wavelength<382,:);
CM4_382 = spec_data(spec_data.wavelength>382 & spec_data.wavelength<383,:);
CM4_383 = spec_data(spec_data.wavelength>383 & spec_data.wavelength<384,:);
etc. Plus I need to save each of those tables.
Thank you for your help!
채택된 답변
the cyclist
2019년 9월 13일
I would use a cell array, not variable names that also encode an index:
for ic = 380:780
CM4{ic} = spec_data(spec_data.wavelength>ic & spec_data.wavelength<(ic+1),:);
end
댓글 수: 10
not working
Well, given the tiny amount of information you gave in your question, I would say that it certainly does work.
spec_data = struct('wavelength',381.5);
for ic = 380:780
CM4{ic} = spec_data(spec_data.wavelength>ic & spec_data.wavelength<(ic+1),:);
end
That code results in exactly what I would expect.
So, instead of just saying "not working", it would be better if you gave more context, uploaded your full code (or at least a portion that we can run), and the full text of any error message you are getting.
Sorry. Here is my entire code:
load raw_CM4_19.mat
numfiles = length(txtFiles);
spec_data = [];
for k = 1:numfiles
spec = table(txtFiles(k).wavelength,txtFiles(k).irradiance,'VariableNames',{'wavelength','irradiance'});
spec = spec(spec.wavelength>380 & spec.wavelength<781,:);
spec_data = vertcat(spec_data,spec);
end
for ic = 380:780
CM4{ic} = spec_data(spec_data.wavelength>ic & spec_data.wavelength<(ic+1),:);
end
the cyclist
2019년 9월 13일
I see that my code does indeed give an error here. But for the MAT file you uploaded, spec_data is empty, because numfiles is 0, because txtFiles is empty.
So, it's not a good test case, it seems.
vicsm
2019년 9월 13일
Well, that's weird because for me it's working just fine...spec_data is 54672x2, because numfiles is 48, because txtFiles is a 48x1 structure.
the cyclist
2019년 9월 13일
Are you sure you uploaded the file you intended to? In your code, it is called raw_CM_19.mat, but the file you uploaded is raw_CM4_19.mat.
vicsm
2019년 9월 13일
Thank you for noticing. Indeed, I uploaded the wrong one. Comment edited!
the cyclist
2019년 9월 13일
So, is there still a problem? Your code runs to completion for me, and fills in the cells I would expect.
I would suggest
for ic = 380:780
CM4{ic-380+1} = spec_data(spec_data.wavelength>ic & spec_data.wavelength<(ic+1),:);
end
unless you have a use for CM4{1} to CM4{379}
vicsm
2019년 9월 13일
I does work, thank you!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
태그
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
