Text Output to File

3 次查看(过去 30 天)
Amanda
Amanda 2013-2-12
Trying to achieve an output to a textfile as seen below:
x1 y1 series
1 1 174.08
2 1 174.08
3 1 174.08
4 1 174.08
5 1 174.08
Instead I'm getting:
x1 y1 series
1 2 3
4 5 1
1 1 1
1 174.085
Here is my code:
x1 = [1 2 3 4 5];
y1 = [1 1 1 1 1];
handles = [];
fid = fopen('filename.txt','w+')
g1 = plot(x,y)
h1 = findobj(g1,'Type','line')
x = get(h1,'xdata')
y = get(h1,'ydata')
axis equal;
handles(1) = h1;
set(g1,'ButtonDownFcn',{@ButtonClick,h1});
fprintf(fid,'%s\t %s\t %s\n', 'x1', ' y1','series');
fprintf(fid,'%g\t %g\t %f\n' ,x, y, h1);
fclose(fid)
Thanks, Amanda

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2013-2-12
x1 = [1 2 3 4 5];
y1 = [1 1 1 1 1];
handles = [];
fid = fopen('filename.txt','w+')
g1 = plot(x1,y1)
h1 = findobj(g1,'Type','line')
x = get(h1,'xdata')
y = get(h1,'ydata')
xy=[x; y; repmat(h1,1,numel(x))]
axis equal;
handles(1) = h1;
set(g1,'ButtonDownFcn',{@ButtonClick,h1});
fprintf(fid,'%s\t %s\t %s\n', 'x1', ' y1','series');
fprintf(fid,'%g\t %g\t %f\n' ,xy);
fclose(fid)

更多回答(1 个)

Walter Roberson
Walter Roberson 2013-2-12
fprintf(fid,'%g\t %g\t %f\n', [x; y; h1]);

类别

Help CenterFile Exchange 中查找有关 Language Fundamentals 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by