################################### ### ### ### MATLAB ### ### ### ################################### ======================================== ======== read from keyboard ========= ======================================== a = input('give a number: '); % for ex. 12 Enter v = input('give a vector: '); % for ex. [ 1 2 3] Enter s = input('give some text (in single quotes): '); % for ex. 'abc' Enter b = input('give two numbers: '); c = input(''); % for ex. 3 Enter -8.1 Enter ======================================== ======== print to screen =========== ======================================== disp('input data were:'); disp(s), disp(a), disp(v); disp(['all in one line: ', s, ' ', num2str(a), ', ', num2str(v)]); a=3; disp(a); disp('a'); disp('abcd'); ======================================= ========= advanced print ========= ======================================= fprintf('abcd'); % new line fprintf('ab\ncd\n'); % printing numbers (in combination with text) fprintf('whole number %d, decimal number %f \n', 12, 14); % format repeats itself, till all the numbers are printed: fprintf('whole number %d, decimal number %f \n', 12, 14, 123, 7, 5); fprintf('formatting numbers: %6.2f, %.4f, %.3e \n', 1, 2, 35); % the whole vector can be printed at once y = 2:0.1:3; fprintf(' %f, ', y); % matrix printing - by columns: x = 0:0.1:1; y = [x; exp(x)]; fprintf('%6.2f %12.8f\n', y); % number of bytes (characters) printed: nb = fprintf('%d, %d, \n', -6, 40) ======================================= ========= print to a file ========= ======================================= ============ easy print ================ save('vysl_y.txt', 'y', '-ascii') % matrix y is printed to file vysl.txt (as ascii text) ==== standard print ==================== fid = fopen('vysl.txt','w'); % open file vysl.txt for writing % for Windows it should be better to use: fid = fopen('vysl.txt','wt'); fprintf(fid,'results %d\n', 1); % write to file vysl.txt y = 2:0.1:3; fprintf(fid,'%6.2f %12.8f\n',y); % write to file vysl.txt fclose(fid); % close file vysl.txt ======================================= ========= reading from a file ======== ======================================= ========= easy reading ================ A = load('vysl_y.txt','-ascii'); % can be used only if there is some matrix in the file, for instance % 1 -5 6.1 % -2.16 4.71 2 % 16 20 11 % -3.6 6 6 % reads matrix from the file and stores it in the variable A % (so in the last example, the first line in the file vysl.txt must be erased) % % if file has .txt suffix, you need not use '-ascii' in the command ==== standard reading =================== fid = fopen('vysl.txt','r'); % open file vysl.txt for reading a = fscanf(fid, '%s', 2); % read the first two words as text (skipping spaces) b = fscanf(fid,'%f',4); % read following 4 numbers - result is a vector % n will contain the number of successfully read values (2 here): [c, n] = fscanf(fid,'%f',2); % read until the end of file (or error) occurs: [d, n] = fscanf(fid,'%f'); % reshapping of the vector to original shape of 2 columns (use 'help reshape') y = reshape(d, 2, n/2)'; fclose(fid); % close file vysl.txt ==========================================================