필터 지우기
필터 지우기

Variable Output to Text File

조회 수: 3 (최근 30일)
Amanda
Amanda 2013년 2월 18일
I have 3 columns of data(3X1) saved in CA, TX, FL variables:
CA = [1;2;3]
TX = [4;5;6]
FL = [7;8;9]
I want to generate an output to a* text file* as seen below:
CA TX FL
1 4 7
2 5 8
3 6 9
I have been working with various ways fprintf and etc, but the rows and columns are not lining up. Here is one idea that I have been working with:
Thanks, Amanda

채택된 답변

the cyclist
the cyclist 2013년 2월 18일
편집: the cyclist 2013년 2월 18일
It wasn't clear to me if you wanted to include the state abbreviations as headers to the columns, so I included a flag that you set true/false.
There are probably slicker ways to do this, but I tried to stick close to the format you had, to help your understanding.
INCLUDE_HEADER = true;
CA = [1;2;3]
TX = [4;5;6]
FL = [7;8;9]
myMatrix = [CA,TX,FL];
mycell = num2cell(myMatrix);
myHeader = {'CA','TX','FL'};
[nrows,ncols] = size(mycell)
filename = 'celldata.txt'
fid = fopen(filename,'w')
if INCLUDE_HEADER
fprintf(fid,'%s %s %s\n', myHeader{:});
end
for row=1:nrows
fprintf(fid,'%d %d %d\n', mycell{row,:});
end
fclose(fid)
  댓글 수: 1
Amanda
Amanda 2013년 2월 18일
Thanks so much. I have been struggling on this all morning.

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 2월 18일
편집: Azzi Abdelmalek 2013년 2월 18일
Try
dlmwrite('filename',cell2mat(mycell),'delimiter',' ')

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by