Anglesharp get p , h and img tags - queryselector

I want to take h p and img element from HTML page but I didn't find how can I do this on anglesharp.I can take all p tags but I want to take all p h and img element respectively.
I can take like this :
var config = Configuration.Default.WithDefaultLoader();
var context = BrowsingContext.New(config);
var document = await context.OpenAsync("any_site");
var elements = document.Body.QuerySelectorAll("p");
I try this but does not work
var elements = document.Body.QuerySelectorAll("p h1 h2 h3 h4 h5 img");
How can I do this ?

Use a comma to separate different selections:
var elements = document.Body.QuerySelectorAll("p, h1, h2, h3, h4, h5, img");
Just having a space is the descendants selector, which looks in the pool of children and their children recursively.

Related

Format a DOM MATLAB table

Based on this question: Table fields in format in PDF report generator - Matlab, I converted the p table to be a DOM MATLAB Table. How can I color the header row, remove the underlining of its entries and apply the tableStyle on the rest of it.
With my code, I get all the table with the LightBlue color and the tableStyle is not applied.
Code:
function ButtonPushed(app, event)
import mlreportgen.dom.*;
import mlreportgen.report.*
ID = [1;2;3;4;5];
Name = {'San';'John';'Lee';'Boo';'Jay'};
Index1 = [1;2;3;4;5];
Index2 = [176.23423;163.123423654;131.45364572;133.5789435;119.63575647];
Index3 = [176.234;16.123423654;31.45364572;33.5789435;11.6647];
p = table(ID,Name,f(Index1),f(Index2),f(Index3));
V = #(x) inputname(1);
p.Properties.VariableNames(3:end) = {V(Index1), V(Index2), V(Index3)};
headerStyle = { BackgroundColor("LightBlue"), ...
Bold(true) };
tableStyle = { Width("60%"), ...
Border("single"), ...
RowSep("solid"), ...
ColSep("solid") };
p = MATLABTable(p)
p.Style = headerStyle;
p.TableEntriesHAlign = 'center';
d = Document('myPDF','pdf');
d.OutputPath = ['E:/','temp'];
append(d,'Report for file: ');
append(d,p);
close(d);
rptview(d.OutputPath);
end
function FivDigsStr = f(x)
%formatting to character array with 5 significant digits and then splitting at each tab
FivDigsStr = categorical(split(sprintf('%0.5G\t',x)));
%Removing the last (empty) value (which is included due to \t)
FivDigsStr = FivDigsStr(1:end-1);
end
You defined the variable tableStyle in which you defined the desired properties but you didn't set them. Also instead of setting the header as bold and light blue background, you are setting all the entries to have that.
The following code fixes that:
set(p, 'Width', '60%', 'Border', 'single',...
'RowSep', 'solid', 'ColSep', 'solid',...
'TableEntriesHAlign', 'center');
p.HeaderRule = ''; %To remove the underlining of header entries
p.Header.Style = headerStyle; %Setting the Header Style

Given an x coordinate, how do I delete a line from an array of lines which has that x coordinate

In matlab, I have a bunch of stored lines that are just vertical in an array called H, like so:
h(1)=plot([10,10][750,1000])
h(2)=plot([20,20][750,1000])
h(3)=plot([30,30][750,1000])
I know that to delete the second plot, i would do: delete(h(2)) followed by h(2)=[]. The problem is, I don't know the index of 20. Let's say I have the number 20 stored, is there a way to get the location of my vector h where there is a line with an x value of 20 to delete?
You can do it as follows:
h(2).XData(20) = [];
h(2).YData(20) = [];
Example:
X = 1:5;Y = 1:5;
h = plot(X, Y, 'o');grid on;
h.XData(3) = [];h.YData(3) = [];

feature extraction using vl_feat toolbox

imshow(imread(a));
img = single(imread(a));
[f,d] = vl_phow(img);
perm = randperm(size(f,2)) ;
s = perm(1:50)
h1= vl_plotframe(f(:,s));
h2= vl_plotframe(f(:,s));
set(h1,'color','k','linewidth',3) ;
set(h2,'color','y','linewidth',2) ;
h3 = vl_plotsiftdescriptor(f(:,s),d(:,s)) ;
set(h3,'color','g') ;
But when I try to plot them using vl_plotsiftdescriptors, it gives an error.
whos
d 128x3692 uint8
f 4x3692 double
The error is:
The number of rows of D does not match the geometry of the descriptor
Could someone please help me with this?
Am I doing it the right way?
Thanks in advance.
The problem is in the line
h3 = vl_plotsiftdescriptor(f(:,s),d(:,s)) ;
with f(:,s) and d(:,s), you are loading the s-th row of f and s, while the VLFeat documentation states, that
Each column of D is the descriptor of the corresponding frame in F.
So to extract the descriptors at index s, you'll instead have to call
h3 = vl_plotsiftdescriptor(f(s,:),d(s,:)) ;
--
Additional comment: By using the second parameter (k) of randperm, you can replace
perm = randperm(size(f,2)) ;
s = perm(1:50);
by
s = randperm(size(f,2), 50);
which should be faster, as you only generate 50 random numbers, instead of generating size(f,2) numbers and discarding most of them.

How do I copy fields names and their contents to another struct variable in matlab

Lets say that I have this code:
a=struct;
a(1).a='a1';
a(1).b='b1';
a(1).c='c1';
a(2).d='a1';
a(2).e='b1';
a(2).f='c1';
a(3).g='a1';
a(3).h='b1';
a(3).i='c1';
Now, I want to copy only a(2) to b(1) with its all fields, but I don't know what fields are in a. for example:
b=struct;
b(1)=a(2);
b(2)=a(3);
(if I do that I get the error:
'Subscripted assignment between dissimilar structures.')
How can I do that?
Let s1 be the input struct and s2 be the desired output struct. Here's one approach based on struct2cell and cell2struct to get s2 -
%// Given input struct
s1=struct;
s1(1).a='A';
s1(1).b='B';
s1(1).c='C';
s1(2).d='D';
s1(2).e='E';
s1(2).f='F';
s1(3).g='G';
s1(3).h='H';
s1(3).i='I';
idx = [2 3]; %// indices of struct data to be copied over from s1 to s2
fn = fieldnames(s1) %// get fieldnames
s1c = struct2cell(s1) %// Convert s1 to its cell array equivalent
%// Row indices for the cell array that has non-empty cells for the given idx
valid_rowidx = ~all(cellfun('isempty',s1c(:,:,idx)),3)
%// Construct output struct
s2 = cell2struct(s1c(valid_rowidx,:,idx),fn(valid_rowidx))
Thus, you would end up with the output -
s2 =
1x2 struct array with fields:
d
e
f
g
h
i
Finally, you can verify the contents of the output struct like so -
%// Verify results
check1 = [s1(2).d s1(2).e s1(2).f]
check2 = [s2(1).d s2(1).e s2(1).f]
check3 = [s1(3).g s1(3).h s1(3).i]
check4 = [s2(2).g s2(2).h s2(2).i]
which yields -
check1 =
DEF
check2 =
DEF
check3 =
GHI
check4 =
GHI
Don't declare b. Then use deal:
a=struct;
a(1).a='a1';
a(1).b='b1';
a(1).c='c1';
a(2).d='a1';
a(2).e='b1';
a(2).f='c1';
a(3).g='a1';
a(3).h='b1';
a(3).i='c1';
b(1) = deal(a(2));
b(2) = deal(a(3));
If you declare b before using deal, you will have to declare b as a struct with all fields which a has. In this case you don't need 'deal' anymore, just assign them normally as you did.

How do I run a foreach field in a class/ struct in matlab?

This is my class : (direction) :
classdef direction
properties
up = zeros(4,5)
down = zeros(4,5)
left = zeros(4,5)
right = zeros(4,5)
end
%%%
methods
end
end
I want to be able to run a
for each field in 'direction'
do something
but I don't know how to use it.
Now I'm using
ROAD.up = ...
but I'll want more fields at the end (16 or 32)
I try now a struct solution :
I'm using at the moment at
road(1).direction
and etc
but I find the class solution more right...
My first guess is you might be interested in structfun
Theoretically it should work with classes as well - practically I find matlab classes unpredictable.
Get the properties and loop over them:
d = direction
p = properties(d)
for k = 1:length(p)
prop = p{k};
d.(prop) = k
end
For example, the above code would start with:
d =
direction
Properties:
up: [4x5 double]
down: [4x5 double]
left: [4x5 double]
right: [4x5 double]
and result in:
d =
direction
Properties:
up: 1
down: 2
left: 3
right: 4
If you want to specify the list yourself, you can use a cell array of strings and use the obj.('name') operator:
p = {'up', 'down', 'left', 'right'};
k = 2; % Have a loop here instead
d.(p{k}) = 5; % Set property value
You can roll your own function that applies functions to object fields, analagous to structfun.
function out = objfieldfun(x, fcn)
%OBJFIELDFUN Apply a function to every field of an object
out = x;
fields = fieldnames(x);
for iX = 1:numel(x)
for iField = 1:numel(fields)
out(iX).(fields{iField}) = feval(fcn, x(iX).(fields{iField}));
end
end
Then you can use it like this.
d = direction;
d2 = objfieldfun(d, #(x)x+2);
But... usually objects' named properties have particular meanings and roles, and it'd be unusual to apply the same operation to all fields. Maybe it would make more sense to stash the similar properties inside a struct which itself is in a field on the object.