Save multiple variables into single -Ascii File with different column of [x:y:z]

조회 수: 21 (최근 30일)
Hi, I want to save the variables into 3 column in a single .ascii file.
The dimension of each variables are same which is
SSS (102600x1)
Lat (102600x1)
Lon (102600x1)
I want to save into X:Y:Z matrix, where X=Lat, Y=Lon, Z=SSS in one single file of .ascii
Here I attach my coding, the problem is when I run this code, the 20150131.asc is not arrange into X:Y:Z, instead only one column, I believed there is problem in the code of save.
clear all
clc
ncfile = 'SM_REPR_MIR_OSUDP2_20150131T230416_20150131T235723_700_200_1.nc' ; % nc file name
% To get information about the nc file
ncinfo(ncfile)
% % to display nc file
ncdisp(ncfile)
% % to read a vriable 'var' exisiting in nc file
SSS = ncread(ncfile,'SSS_corr') ;
Lat = ncread(ncfile, 'Latitude');
Lon = ncread(ncfile, 'Longitude');
save('20150131.asc','SSS','Lat','Lon','-ASCII');

채택된 답변

Rik
Rik 2023년 3월 22일
편집: Rik 2023년 3월 22일
Either use writematrix, or use fprintf. See the documentation for examples.
If you use fprintf, be aware that it will go through an array column by column, but you read a file row by row.
Edit: here are examples for both.
SSS = rand(10,1);
Lat = rand(10,1)+1;
Lon = rand(10,1)+2;
writematrix([SSS Lat Lon],'test.txt');
type test.txt
0.669445437975914,1.10818043456691,2.6331452154219 0.907265175149237,1.52101179043433,2.82738085740537 0.505780586348896,1.29246069636971,2.0528117599317 0.374943908267921,1.79577907919819,2.36857528237917 0.686581700252366,1.80675349507712,2.19810055267167 0.96998566573091,1.07417119089903,2.58011393382802 0.4378440260847,1.23686004157163,2.53002051995761 0.101907567450226,1.40448472931069,2.04594865016132 0.168935387402048,1.03050153648287,2.25384510163555 0.994842216687662,1.44955758038263,2.47677849399508
fid = fopen('test2.txt','w');
fprintf(fid,'%.1f %.1f %.1f\n',[SSS,Lat,Lon].');
fclose(fid);
type test2.txt
0.7 1.1 2.6 0.9 1.5 2.8 0.5 1.3 2.1 0.4 1.8 2.4 0.7 1.8 2.2 1.0 1.1 2.6 0.4 1.2 2.5 0.1 1.4 2.0 0.2 1.0 2.3 1.0 1.4 2.5
  댓글 수: 3
Rik
Rik 2023년 3월 22일
I have edited my answer, but the documentation pages for both functions also contain examples. You should really try to adapt those. That will help you solve your question without having to wait for us to respond.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 3월 22일
out = [SSS, Lat, Lon];
save('20150131.asc', 'out', '-ascii')

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by