이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
My code pulls some data out of binary files and puts this data into an Array. When the code is done running I can see this Array in the Workspace window. However, When I turn my code into a function, once executed, I can not see the resulting Array int the Workspace.
function getchan(wd)
files = dir('*.bin');
DATA=[];
tic;
for i=1:100 %length(files)
[d] = readbin (files(i).name);
data=(d(:,1));
DATA=[DATA;data]
end
toc;
end
I also would like to be able to call this function and provide the directory to execute on as in getchan('directory') however, with code as is now, I have to make that directory the current directory first and then run the function by typing getchan
채택된 답변
Sven
2011년 11월 15일
1 개 추천
Baba, variables created inside a function are limited to that function's scope. If you want to have DATA available outside the function, you should make that function return the DATA variable:
function DATA = getchan(wd)
% Search for .bin files in the wd directory
files = dir(fullfile(wd,'*.bin'));
DATA=[];
% Loop over every .bin file and build up DATA
for i=1:length(files)
% Make sure we're reading from the wd directory
d = readbin (fullfile(wd,files(i).name));
% Append the first column of what we read to DATA
DATA=[DATA; d(:,1)];
end
end
Now you can simply call:
MY_DATA = getchan(pwd)
Note above that I've also used "fullfile", passing in "wd" from your function to make your function work specifically on the directory stored in "wd".
댓글 수: 10
Baba
2011년 11월 15일
why is it when I call MY_DATA = getchan(pwd);, i'm unable to supress the output?
Baba
2011년 11월 15일
Also, can you make a few comments about the changes that you made? thank you!
Walter Roberson
2011년 11월 15일
Baba, put a semi-colon on the end of the line
DATA=[DATA;data]
Sven
2011년 11월 16일
Hi Baba,
Yes, I tried to keep as much as possible from your original question the same so that you could easily see the 3 changed lines:
function DATA = getchan(wd)
files = dir(fullfile(wd,'*.bin'));
and
d = readbin (fullfile(wd,files(i).name));
I've now updated my answer to be a slightly "cleaner" version from your original. Note that I've:
1. Set DATA as a function output (first line)
2. Used "wd" when searching for *.bin files.
3. Looped over every *.bin file (you had commented this out and limited to 100)
4. Removed the tic() toc() calls (you can now call tic/toc from outside the function if you want timing).
5. Shortened the sequence of storing "d" then "data" then "DATA", simply storing "d" then "DATA".
I hope this answers your question well.
Baba
2011년 11월 16일
ok great thank you. by the way, even though DATA is now showing up in the workspace just like I wanted, DATA is still not suppressed and shows up in the command window. Despite the DATA=[DATA; d(:,1)];
line being terminated with a semicolon
Sven
2011년 11월 16일
*Any* line that returns a result will be displayed to the command window *unless* it has a semi-colon ";" character after it.
What about your actual call to "getchan"... are you using:
MY_DATA = getchan(pwd)
or
MY_DATA = getchan(pwd);
Baba
2011년 11월 16일
ahhh that was a silly thing to ask. thank you. I'm really very new to programming and know that I'm asking a tonn of silly questions. but hopefully things click soon.
Baba
2011년 11월 16일
Another question about the code above: when the program looks at the bin files in this line: files = dir(fullfile(wd,'*.bin'));
does it sort them by name and then processes?
Sven
2011년 11월 16일
Baba, try:
doc dir
It says:
"... Results appear in the order returned by the operating system."
Sven
2011년 11월 16일
Don't worry, you're allowed to be new to programming. You'll get good help here especially if you ask clear, concise questions including the code you're using.
I've noticed one or two of your questions that are answered by the MATLAB documentation - that might be a useful source. For example, if your next question is, say, about how to order the results from the dir() command, then searching the docs for "sort" will almost certainly answer your question.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
태그
참고 항목
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)
