Displaying in command winodw but i need to store in .txt file

조회 수: 1 (최근 30일)
joy
joy 2012년 10월 30일
Hello
In a code I have a random 20000 data sequence ranging form 0 to 255, I am displaying the locations of each data they occurred in a matlab code in command window such that
for i=0:1:255
i
locations=find(X==i)% finding the locations of same number in the randomly genarated vector
% X is my random generated sequence of 20000 data
I am getting output in command window such that
i =
134
locations =
Columns 1 through 10
402 802 846 1476 2101 2293 2322 2594 3408 3444
that is 134 occurs in such positions of random sequence
but due to huge no of data the complete list is not available on command window i am getting data from i=125 the rest of data are hiding some where in command window,,how can I put this i and locations in a separate notepad file.. regards

채택된 답변

Sean de Wolski
Sean de Wolski 2012년 10월 30일
You could use diary:
doc diary
Or fopen/fprintf/fclose
  댓글 수: 3
Jonathan Epperl
Jonathan Epperl 2012년 10월 30일
편집: Jonathan Epperl 2012년 10월 30일
Use the diary suggestion, it doesn't get any easier than that!
diary mydiaryfile.txt
%%Your Code %%
diary off
and now you have a file mydiaryfile.txt in your current working directory with all the output in it.
joy
joy 2012년 10월 31일
yes Sean de Wolsk & Jonathan Epperl
Diary really works for me, thanx

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Kye Taylor
Kye Taylor 2012년 10월 30일
편집: Kye Taylor 2012년 10월 30일
The following code is not ideal in that two for loops are being used where one would suffice. However, it makes it clear which code is creating data, and which code is writing to a file.
X = randi(256,1,1000) - 1;% proxy for your X
v = 0:1:255; % vector of values
locations = cell(1,length(v)); % space for each vector of indices
for i = 1:length(v)
locations{i}=find(X == v(i));
end
[fid,msg] = fopen('newTextFile.txt','w');
fprintf(fid,'%s\t%s\n','idx','loc');
for i = 1:length(v)
array = [i,locations{i}];
fprintf(fid,[repmat('%d\t',1,length(array)),'\n'],array);
end
fclose(fid);

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by