-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stonks.m
273 lines (273 loc) · 12.8 KB
/
Stonks.m
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
%Program to display stock portfolio, note that the first 2 numbers in the
%data must be the number of rows and then the number of columns.
%Input file cannot have spaces after lines.
%Note that if you have a choice, it is probably better to ammend your
%current portfolio yourself in the txt file, as you can change multiple
%things at once.
%This program cannot handle poorly entred input, be gentle.
fprintf('Note: Use '' '' or " " when entering your values on lines starting with !\n');
nameoffile = input('! Enter the name of the file containg your portfolio: ');
data = fopen(nameoffile,'r');
nrowcol = fscanf(data,'%d',[1,2]);%row is the first, cols is the second
ourcell = cell(nrowcol);
fgetl(data);
for i = 1:nrowcol(1)
line = fgetl(data);
line = strsplit(line);
ourcell(i,:) = line;
end
while true
fprintf('\nWhat would you like to do today?\nCurrent options:\n');
fprintf('1: View current data\n');
fprintf('2: Modify current data\n');
fprintf('3: Display a chart\n');
fprintf('4: Exit\n')
choice = input('Select an option: ');
fprintf('\n');
switch choice
case 1%Finished and functional
disp(nrowcol);
disp(ourcell);
case 2%Works fine
fprintf('Your choices are:\n');
fprintf('1: Modify a current entry\n');
fprintf('2: Add new entry\n');
fprintf('3: Delete an entry\n');
newchoice = input('Select an option: ');
fprintf('\n');
switch newchoice
case 1%Ammend, works fine
disp(ourcell);
line = input('Which line would you like to modify? ');
disp(ourcell(line+1,:));
modify = input('! Which value would you like to change? Enter the current value: ');
for m = 1:nrowcol(2)%This finds which category needs to be ammended
if string(cell2mat(ourcell(line+1,m)))==string(modify)
break
end
end
newval = input('! Enter the new value: ');
ourcell{line+1,m} = char(newval);%Maybe neccessary for the exe version (char)
choice2 = input('! Enter the new file name, or type ''date'' to generate a name based on the date and time: ');
if (string(choice2) == "date")
ourdate = string(clock);
ourdate = strcat(ourdate(1),'-',ourdate(2),'-',ourdate(3),'_',ourdate(4),'-',ourdate(5),'.txt');
else
ourdate = string(choice2);
end
fprintf('Your new data is named %s\n\n',ourdate);
output = fopen(char(ourdate),'w');
fprintf(output,'%d %d\n',nrowcol(1),nrowcol(2));
for k = 1:nrowcol(1)
fprintf(output,'%s ',ourcell{k,:});
fseek(output,-1,'cof');%If we read from this file again, the spaces produced above screw with it
fprintf(output,'\n');%This way, we sneak in a \n before the space, resulting in functional behaviour
end
fclose(output);
case 2
newentry = cell(1,nrowcol(2));
fprintf('Type the values for the given category\n');
ourcell(nrowcol(1)+1,1) = {char(num2str(nrowcol(1)+1))};
for j = 2:nrowcol(2)
fprintf('! %s:',ourcell{1,j});
entry = input(' ');
ourcell(nrowcol(1)+1,j) = {char(entry)};
end
choice2 = input('! Enter the new file name, or type ''date'' to generate a name based on the date and time: ');
if (string(choice2) == "date")
ourdate = string(clock);
ourdate = strcat(ourdate(1),'-',ourdate(2),'-',ourdate(3),'_',ourdate(4),'-',ourdate(5),'.txt');
else
ourdate = string(choice2);
end
fprintf('Your new data is named %s\n\n',ourdate);
output = fopen(char(ourdate),'w');
nrowcol(1) = nrowcol(1)+1;
fprintf(output,'%d %d\n',nrowcol(1),nrowcol(2));
for k = 1:nrowcol(1)
fprintf(output,'%s ',ourcell{k,:});
fseek(output,-1,'cof');%If we read from this file again, the spaces produced above screw with it
fprintf(output,'\n');%This way, we sneak in a \n before the space, resulting in functional behaviour
end
fprintf(output,'%s',newentry{1,:});
fclose(output);
case 3
disp(ourcell);
deletedline = input('Which line would you like to remove? ');
for m = nrowcol(1):-1:deletedline+1
ourcell{m,1} = num2str(str2double(ourcell(m,1))-1);
end
ourcell(deletedline+1,:) = [];
choice2 = input('! Enter the new file name, or type ''date'' to generate a name based on the date and time: ');
if (string(choice2) == "date")
ourdate = string(clock);
ourdate = strcat(ourdate(1),'-',ourdate(2),'-',ourdate(3),'_',ourdate(4),'-',ourdate(5),'.txt');
else
ourdate = string(choice2);
end
fprintf('Your new data is named %s\n\n',ourdate);
output = fopen(char(ourdate),'w');
nrowcol(1) = nrowcol(1)-1;
fprintf(output,'%d %d\n',nrowcol(1),nrowcol(2));
for k = 1:nrowcol(1)-1
fprintf(output,'%s ',ourcell{k,:});
fseek(output,-1,'cof');%If we read from this file again, the spaces produced above screw with it
fprintf(output,'\n');%This way, we sneak in a \n before the space, resulting in functional behaviour
end
otherwise
fprintf('I couldn''t understand that. Try again\n\n');
end
case 3
fprintf('Choose one of the following:\n');
fprintf('1: Printing a column\n');
fprintf('2: Category filtering\n');
fprintf('3: Navigating the table\n');
newerchoice = input('Make a choice ');
disp(ourcell);
switch newerchoice
case 1
sizes = size(ourcell);
selections = ourcell;
selections(1,:) = [];
col = input('! Type the name of the column to display: ');
for v1 = 1:sizes(2)
if string(col) == string(ourcell(1,v1))
break;
end
end
options = categories(categorical(selections(:,v1)));
soptions = size(options);
sums4opt = zeros(soptions(1),1);
for p = 1:sizes(1)-1
for z = 1:soptions(1)
if string(options(z)) == string(selections(p,v1))
sums4opt(z) = sums4opt(z) + str2double(cell2mat(selections(p,sizes(2))));
end
end
end
g = pie(sums4opt);
%manipulating the graph
colonspace = cell(1,soptions(1));
for w = 1:soptions(1)
colonspace(w) = {': '};
end
pText = findobj(g,'Type','text');
percents = get(pText,'String');%ideally, this should allow the names to appear with the %
combine = strcat(options(:),string(colonspace)',percents);
for w = 1:soptions(1)
pText(w).String = combine(w);
end
title(string(ourcell(1,v1)));
case 2%group same tickers done
selections = ourcell;
selections(1,:) = [];
sizes = size(selections);
fprintf('Sample input looks like: ''Canada !Stock Telecom'', where ! represents negation.\n');
wholething = input('! Enter a list of types you want to inlude/disclude.\n');
keys = strsplit(wholething);
allowedvals = true(sizes(1),1);
for qq = 1:length(keys)
temp = char(keys(qq));
if temp(1) == '!'
vals = (string(selections) == string(temp(2:length(temp))));
[x,y] = find(vals);
vals(:,y(1)) = ~vals(:,y(1));
else
vals = (string(selections) == string(temp));
[x,y] = find(vals);
end
allowedvals = (allowedvals & vals(:,y(1)));
end%At this point we have the rows that fit the given criteria
a = (allowedvals.*str2double(selections(:,nrowcol(2))));%produces an array. If the value is 0, remove it
a = [a string(selections(:,nrowcol(2)-1))];
for r = nrowcol(1)-1:-1:1
if a(r,1) == "0"
a(r,:) = [];
end
end
if isempty(a)
fprintf('Nothing fits the given criteria.\n\n')
clearvars
end
options = categories(categorical(a(:,2)));
soptions = size(options);
sizea = size(a);
sums4opt = zeros(soptions(1),1);
for p = 1:sizea(1)
for z = 1:soptions(1)
if string(options(z)) == string(a(p,2))
sums4opt(z) = sums4opt(z) + str2double(cell2mat(selections(p,sizes(2))));
end
end
end
g = pie(sums4opt);
colonspace = cell(1,soptions(1));
for w = 1:soptions(1)
colonspace(w) = {': '};
end
pText = findobj(g,'Type','text');
percents = get(pText,'String');%ideally, this should allow the names to appear with the %
combine = strcat(options(:),string(colonspace)',percents);
for w = 1:soptions(1)
pText(w).String = combine(w);
end
title(wholething);
case 3
selections = ourcell;
selections(1,:) = [];
for v = 2:nrowcol(2)
sizes = size(selections);
options = categories(categorical(selections(:,v)));
fprintf('Category %d: %s\n',v,string(ourcell(1,v)));
fprintf('Type ''print'' to print these selections ''skip'' to disregard this column, or choose another category\n');
fprintf('%s\n',string(options));
choix = input('! Enter your choice: ');
if string(choix) == "print"
break
end
if string(choix) == "skip"
continue
end
for u = sizes(1):-1:1
if(string(choix) ~= string(selections(u,v)))
selections(u,:) = [];
end
end
end
%here we print/pie our selections, based on the next categories
soptions = size(options);
sums4opt = zeros(soptions(1),1);
for p = 1:sizes(1)
for z = 1:soptions(1)
if string(options(z)) == string(selections(p,v))
sums4opt(z) = sums4opt(z) + str2double(cell2mat(selections(p,sizes(2))));
end
end
end
g = pie(sums4opt);
%manipulating the graph
colonspace = cell(1,soptions(1));
for w = 1:soptions(1)
colonspace(w) = {': '};
end
pText = findobj(g,'Type','text');
percents = get(pText,'String');%ideally, this should allow the names to appear with the %
combine = strcat(options(:),string(colonspace)',percents);
for w = 1:soptions(1)
pText(w).String = combine(w);
end
title(string(ourcell(1,v)));
otherwise
fprintf('I couldn''t understand that. Try again.\n\n');
end
case 4
fprintf('Program terminated. Have a nice day!\n');
break;
otherwise
fprintf('I couldn''t understand that. Try again.\n\n');
end
clearvars -except ourcell nrowcol data;
end
fclose(data);
clearvars;
return;