












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of working with files and plotting data in matlab. Topics covered include opening and closing files, reading and writing data, and creating plots using file data. Examples are given for creating and appending data to a file, as well as reading data from a file to create plots.
Typology: Slides
1 / 20
This page cannot be seen from the preview
Don't miss anything!













f = fopen('cs1109.txt','w');
fid = fopen(filename); fid = fopen(filename, permission);
% returns id's of all open files fids = fopen('all');
fprintf(f, '%d %10.5f', n, x);
count = fprintf(fid, format, A, ..);
fclose(f);
status = fclose(fid);
% closes all open files status = fclose('all');
deg = 91:180; rad = deg (^) * pi/180; s = sin(rad);
% let's open a file f = fopen('sintable.txt','w');
% write values to the file for j = 1:length(deg) fprintf(f,'%2d %8.6f\n',deg(j),s(j)); end
% and close it! fclose(f);
deg = 0:90; rad = deg (^) * pi/180; s = sin(rad);
% let's open a file f = fopen('sintable.txt','w');
% write values to the file data = [deg; s]; fprintf(f,'%2d %8.6f\n',data);
% and close it! fclose(f);
% let's open a file for reading f = fopen('sintable.txt','r');
% read values from the file [data, count] = fscanf(f,'%d %f', [2 inf]);
% let's display the values disp(data)
% and close it! fclose(f);
f = fopen('ithaca june 2012.txt','r'); data = fscanf(f, '%d %d %d %d',[4 inf]); fclose(f);
(^600 5 10 15 20 25 )
65
70
75
80
85
90
95
% plot with stars in red color plot(data(1,:),data(2,:),'r*');
(^600 5 10 15 20 25 )
65
70
75
80
85
90
95
% connect data points, use circles plot(data(1,:),data(2,:),'ro−');
figure(1) % plot max temp plot(data(1,:),data(2,:),'r')
figure(2) % plot min temp plot(data(1,:),data(3,:),'b')
figure(3) % plot avg temp plot(data(1,:),data(4,:),'g')
figure hold on plot(data(1,:),data(2,:),'r') plot(data(1,:),data(3,:),'b') plot(data(1,:),data(4,:),'g') hold off % back to normal state
(^400 5 10 15 20 25 )
50
60
70
80
90
100 Ithaca Daily Temperature − June 2012
Days of June 2012
Temperature (F)
title('Ithaca Daily Temperature − June 2012') xlabel('Days of June 2012') ylabel('Temperature (F)')
(^400 5 10 15 20 25 )
50
60
70
80
90
100 Ithaca Daily Temperature − June 2012
Days of June 2012
Temperature (F)
Max Min Avg
legend('Max','Min','Avg')