-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjpa_writeArrayToTxt.m
More file actions
80 lines (74 loc) · 2.03 KB
/
jpa_writeArrayToTxt.m
File metadata and controls
80 lines (74 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
function jpa_writeArrayToTxt(pathToFile, stringArr, direction)
% function that writes a NxM-String-Array in a file under a specified
% direction. You can write out the NxM-String-Array vertical or horizontal.
%
% Syntax: jpa_writeArrayToTxt(pathToFile, stringArr, direction)
%
% Inputs:
% pathToFile - Full Path to a .txt wo be written to
% stringArr - String-Array to be written in file
% direction - direction where to read the String-Array,
% v = vertical, h = horizontal
%
% Outputs:
% .txt - .txt-file containing the fields of stringArr in a certain
% direcotion
%
% Example:
% jpa_writeArrayToTxt('C:\example\examplefile.txt',
% {'line1', 'line2' ; 'line3', 'line4'}, 'v')
% -> Output: line1
% line3
% line2
% line4
%
% jpa_writeArrayToTxt('C:\example\examplefile.txt',
% {'line1', 'line2' ; 'line3', 'line4'}, 'h')
% -> Output: line1
% line2
% line3
% line4
%
% Other m-files required: none
% Subfunctions: none
% MAT-files required: none
%
% See also:
% Author: Jan Albrecht
% Work address: alexander.genauck@charite.de
% email: jan-philipp.albrecht@charite.de, j.p.albrecht@fu-berlin.de
% Website:
% Sep 2015; Last revision: 14-Okt-2015
%------------- BEGIN CODE --------------
% delete file if exist
if exist(pathToFile, 'file')
delete(pathToFile)
end
% open file in txt-Mode
try
outFile = fopen(pathToFile, 'wt');
catch err
error(['Could not open file in path ' pathToFile ' because of reason:' ...
err.message])
end
% get size of Input
[a,b] = size(stringArr);
% print out data
if strcmp(direction, 'h')
for i=1:1:a
for j=1:1:b
fprintf(outFile, '%s\n', stringArr{i,j});
end
end
end
if strcmp(direction, 'v')
for i=1:1:b
for j=1:1:a
fprintf(outFile,'%s\n', stringArr{j,i});
end
end
end
% close file
fclose(outFile);
end
%------------- END CODE --------------