Someone can explain to me how to generate and save a txt, excel file data, after "for loop" and is meshgrid ?

2 views (last 30 days)
Hi,
I have the following script to run the attached data.
fname1 = uigetfile({'*.txt;*.dat;*.tab','Data Files (*.txt,*.dat,*.tab)'; '*.*','All Files (*.*)'},'Ficheiro de dados da Pangaea');
[name,latitude,longitude,depth,agemodel] = textread(fname1,'%s %f %f %f %f','headerlines',1);
agemodel2 = abs(agemodel-20)
LLDA = [latitude,longitude,depth,agemodel,agemodel2]; % Matriz
LLDAn = LLDA(:,1:5); %LLDA(:,2:5)
LLDAm = LLDAn;% cell2mat(LLDAn)
[LatUq, iL, iU] = unique(LLDAm(:,1));
[LonUq, iL, iU] = unique(LLDAm(:,2));
idade_int = zeros(numel(LatUq),numel(LonUq)) * NaN;
for n=1:numel(LonUq)
for m = 1:numel(LatUq)
ind = find(LLDAm(:,2) == LonUq(n) & LLDAm(:,1) == LatUq(m));
if (isempty(ind)), continue; end
%idade_int(n,m) = interp1([0; agemodel(ind)],[0; depth(ind)],20,'linear','extrap');
p = polyfit([0; agemodel(ind)],[0; depth(ind)], 1);
idade_int(m,n) = polyval(p, 20);
end
end
[lon,lat] = meshgrid(LonUq,LatUq)
Is that my results are the following: (for exemple)
lon1 lon2 lon3
lat1 NAN 1 NAN
lat2 NAN NAN 2
lat3 2 NAN NAN
Someone explain to me how to generate and save a txt, excel file data in the following way:
LongUq, LatUq, idade_int
??
Thank you very much Nuno Simões
  3 Comments
Nuno Simões
Nuno Simões on 23 Apr 2014
These are my output results:
latitude =
20.2917 20.2917 20.2917 * _20.2917_ *
20.7492 20.7492 20.7492 20.7492
30.7492 30.7492 30.7492 30.7492
Longitude =
-20.0000 -18.6808 -18.5808 * _20.2917_ *
-20.0000 -18.6808 -18.5808 -18.2617
-20.0000 -18.6808 -18.5808 -18.2617
idade_int =
*Col1 Col2 Col3 Col4*
*Row1* NaN NaN NaN * _1.5621_ *
*Row2* 5.00 5.04 NaN NaN
*Row3* 0.83 NaN 5.00 NaN
Where the columns correspond to the longitudes and latitudes to the rows.
And what I want is to create a file with the following order:
Latitude Longitude idade_int
20.2917 20.2917 1.5621 (Col4, Row1)
Thank you very much!
Nuno Simões
Nuno Simões on 23 Apr 2014
Thanks!
I put dlmwrite in following script:
for n=1:numel(LonUq)
for m = 1:numel(LatUq)
ind = find(LLDAm(:,2) == LonUq(n) & LLDAm(:,1) == LatUq(m));
if (isempty(ind)), continue; end
p = polyfit([0; agemodel(ind)],[0; depth(ind)], 1);
idade_int(m,n) = polyval(p, 20); a = [LatUq(m), LonUq(n), idade_int(m,n)];
disp(a);
dlmwrite('saveteste.txt', a , 'delimiter','\t', 'precision', 4 )
end
end
but when, i record txt file, appears only the last line that was executed in the cycle. While in the command window pops up all the lines.
For example:
Txt file: 20.2917 -18.2617 1.5621
Command Window:
30.7492 -20.0000 0.8333
20.7492 -18.6808 5.0000
20.7492 -18.5808 5.0451
30.7492 -18.5808 5.0000
20.2917 -18.2617 1.5621
20.2917 -18.2617 1.5621
Is there any way to save all lines of the cycle in txt file?

Sign in to comment.

Answers (1)

Lee Albacker
Lee Albacker on 22 Apr 2014
Assuming you want to concatenate the arrays (I'll also assume you can add your own headers if you want):
output = [LongUq, LatUq, idade_int];
To write to an xcell file: xlswrite('FILENAME.xlsx', output); file ends up in your current matlab path
To write to a text file that's tab delimited: dlmwrite('FILENAME.txt', output, 'delimiter', '\t', 'precision', NUMBER OF DECIMAL PLACES)
  1 Comment
Nuno Simões
Nuno Simões on 23 Apr 2014
Thanks!
I put dlmwrite in following script:
for n=1:numel(LonUq)
for m = 1:numel(LatUq)
ind = find(LLDAm(:,2) == LonUq(n) & LLDAm(:,1) == LatUq(m));
if (isempty(ind)), continue; end
p = polyfit([0; agemodel(ind)],[0; depth(ind)], 1);
idade_int(m,n) = polyval(p, 20); a = [LatUq(m), LonUq(n), idade_int(m,n)];
disp(a);
dlmwrite('saveteste.txt', a , 'delimiter','\t', 'precision', 4 )
end
end
but when, i record txt file, appears only the last line that was executed in the cycle. While in the command window pops up all the lines.
For example:
Txt file: 20.2917 -18.2617 1.5621
Command Window:
30.7492 -20.0000 0.8333
20.7492 -18.6808 5.0000
20.7492 -18.5808 5.0451
30.7492 -18.5808 5.0000
20.2917 -18.2617 1.5621
20.2917 -18.2617 1.5621
Is there any way to save all lines of the cycle in txt file?

Sign in to comment.

Categories

Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!