Grouping into a table
2 views (last 30 days)
Show older comments
Hi guys
I have a loop here that returns this:
I want all of these grouped in the same place, one after another... instead of generating a different table each time
This is the code I used to generate it
document1 = "Analyst Actions: Citigroup Cuts Fiverr International's Price Target to $22 From $31, Reiterates Neutral Rating";
document2 = "Analyst Actions: JPMorgan Downgrades Beyond Meat to Neutral From Overweight, Cuts Price Target to $134 From $138";
document3 = "Analyst Actions: Credit Suisse Raises Beyond Meat's Price Target to $125 From $115; Keeps Neutral Rating";
document4 = "Analyst Actions: Oppenheimer Initiates Coverage on Beyond Meat With Perform Rating";
document5 = "Analyst Actions: Morgan Stanley Trims GoPro's Price Target to $3.50 From $4.50, Maintains Underweight Rating";
document6 = "GoPro downgraded by Zacks Investment Research to strong sell. $3.75 PT. (NASDAQ:GPRO)";
document7 = "Analyst Actions: BofA Merrill Lynch Upgrades Fiverr International to Buy From Neutral";
% data is a table containing all documents in a str form
data = table(document1,document2,document3,document4,document5,document6,document7)
issuer = ["JPMorgan";
"Citigroup";
"Credit Suisse";
"Oppenheimer";
"Morgan Stanley";
"Zacks";
"BofA"
"Merrill Lynch";
"Bank of America"];
tblnames = {'Text', 'Issuer'};
sz = (size(data))
rows = sz(1)
columns = sz(2)
N = columns
K = size(issuer)
for idx = 1:N % for as long as idx is 1 to the # of N
scan = data{:,idx}; % scan the documents in data
for i = 1:K % go over all K issuers
if (contains(scan,issuer(i)))
x = issuer(i);
tbl = table(scan,x,'VariableNames',tblnames)
else
"No Match.";
end
end
end
0 Comments
Accepted Answer
Tom Holz
on 4 Mar 2020
Edited: Tom Holz
on 4 Mar 2020
In a situation like this, I usually plan out the table I want to see in advance, and then create variables that I can use to create that structure in a single call to table. In this case, it looks like you want a single table with two columns, Text and Issuer, which you could create in a single call like this, assuming the variables were created correctly.
tbl = table(Text, Issuer);
Here is how I would create those variables:
% Define all the source documents.
documentList = [
"Analyst Actions: Citigroup Cuts Fiverr International's Price Target to $22 From $31, Reiterates Neutral Rating";
"Analyst Actions: JPMorgan Downgrades Beyond Meat to Neutral From Overweight, Cuts Price Target to $134 From $138";
"Analyst Actions: Credit Suisse Raises Beyond Meat's Price Target to $125 From $115; Keeps Neutral Rating";
"Analyst Actions: Oppenheimer Initiates Coverage on Beyond Meat With Perform Rating";
"Analyst Actions: Morgan Stanley Trims GoPro's Price Target to $3.50 From $4.50, Maintains Underweight Rating";
"GoPro downgraded by Zacks Investment Research to strong sell. $3.75 PT. (NASDAQ:GPRO)";
"Analyst Actions: BofA Merrill Lynch Upgrades Fiverr International to Buy From Neutral";
];
% Define all the issuers.
issuerList = [
"JPMorgan";
"Citigroup";
"Credit Suisse";
"Oppenheimer";
"Morgan Stanley";
"Zacks";
"BofA"
"Merrill Lynch";
"Bank of America"
];
% Create variables to store the table columns. The 'Text' column
% is simply the existing document list, but we will have to do
% some work to determine the issuer.
Text = documentList;
Issuer = strings(size(documentList));
% Loop through each issuer.
for k = 1:numel(issuerList)
% Check to see which text(s) contain this issuer.
ind = contains(Text, issuerList(k));
% Assign this issue to the correct rows in the output table.
Issuer(ind) = issuerList(k);
end
% Create the final table.
tbl = table(Text, Issuer);
If you find it confusing to loop over issuers, you can loop over documents instead, like this:
% Loop through each document
for k = 1:numel(documentList)
% Check each issuer.
for j = 1:numel(issuerList)
% Assign the issuer only if it's present in the document string.
if contains(documentList(k), issuerList(j))
Issuer(k) = issuerList(j)
end
end
end
Either way, this leaves you with a 7x2 table with Text in the first column and Issuer in the second.
2 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!