Format a DOM MATLAB table - matlab

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

Related

UI in Matlab to get inputs to be used in a script

I am trying to create a GUI for my script in Matlab. At the moment I am opening this dialog boxes in my script to get the inputs I need.
prompt = {'Mass [kg]:','Power [kW]:','Drag Coeff:','Lift Coeff:','Area Front [m^2]:'};
dlgtitle = 'Vehicle Parameters';
dims = [1 35];
definput = {'752','650','1','3','1'};
vehicle = inputdlg(prompt,dlgtitle,dims,definput);
prompt = {'Friction Coeff:','Air Density [kg/m^3]:'};
dlgtitle = 'External Parameters';
dims = [1 35];
definput = {'1.4','1.19'};
ambient = inputdlg(prompt,dlgtitle,dims,definput);
prompt = {'Length [m]:','Corner Radius [m]:'};
dlgtitle = 'Track Parameters';
dims = [1 35];
definput = {'1000','150'};
track = inputdlg(prompt,dlgtitle,dims,definput);
Here the code continues
laptime = formula used to get the laptime;
However I would like to create something similar to what this image shows, where I can write the parameters, push the run button to run the script and get the laptime printed. Any suggestions on how I could achieve it?
Thanks a lot in advance!
steeven:
From inspecting your image, you essentially will need:
A figure object.
Ten(10) field objects.
Ten(10) Static text objects.
A pushbutton object
A callback function.
Here's a sample script:
clc
clear
close all %Very important when making GUI's in MATLAB!
%figure object that contains GUI.
f = figure('units','normalized');
%Uicontrols devoted to static text:
m_text = uicontrol('Parent',f,'units','normalized','Style','Text');
P_text = uicontrol('Parent',f,'units','normalized','Style','Text');
FS_text = uicontrol('Parent',f,'units','normalized','Style','Text');
CD_text = uicontrol('Parent',f,'units','normalized','Style','Text');
CL_text = uicontrol('Parent',f,'units','normalized','Style','Text');
CF_text = uicontrol('Parent',f,'units','normalized','Style','Text');
rho_text = uicontrol('Parent',f,'units','normalized','Style','Text');
len_text = uicontrol('Parent',f,'units','normalized','Style','Text');
rad_text = uicontrol('Parent',f,'units','normalized','Style','Text');
%Uicontrols devoted to editable fields:
m_edit = uicontrol('Parent',f,'units','normalized','Style','edit');
P_edit = uicontrol('Parent',f,'units','normalized','Style','edit');
FS_edit = uicontrol('Parent',f,'units','normalized','Style','edit');
CD_edit = uicontrol('Parent',f,'units','normalized','Style','edit');
CL_edit = uicontrol('Parent',f,'units','normalized','Style','edit');
CF_edit = uicontrol('Parent',f,'units','normalized','Style','edit');
rho_edit = uicontrol('Parent',f,'units','normalized','Style','edit');
len_edit = uicontrol('Parent',f,'units','normalized','Style','edit');
rad_edit = uicontrol('Parent',f,'units','normalized','Style','edit');
% MLG Cell array strat.
ui_texts = {m_text,P_text,FS_text,CD_text,CL_text,CF_text,rho_text,...
len_text,rad_text};
strings = {'mass','power','Front Section','Drag Coefficient',...
'Lift Coefficient','Friction Coefficient','Air Density','Length',...
'Radius'};
ui_edits = {m_edit,P_edit,FS_edit,CD_edit,CL_edit,CF_edit,rho_edit,...
len_edit,rad_edit};
defaults = {'10','100','0.5','0.05','1.2','0.0005','1.225','5','3'};
xi = 0.05; %X-coordinate of Bottom-Left corner.
yi = 0.85; %Y-coordinate of Bottom-Left corner.
width = 0.15; %Width of object.
height = 0.075; %Height of object
deltaH = 0.3; %Horizontal Spacing between objects.
deltaV = -0.25; %Vertical Spacing between objects.
offset = 0.15;
rows = 3; %Number of rows.
cols = 3; %Number of columns.
k = 0; % Counter for number of uicontrols that populate grid pattern.
for i = 1:rows
for j = 1:cols
k = k + 1;
if k <= length(ui_texts)
x = xi + deltaH*(j-1);
y = yi + deltaV*(i-1);
xo = x + offset; %horizontally offset the editable field.
set(ui_texts{k},'Position',[x, y, width, height],'String',strings{k})
set(ui_edits{k},'Position',[xo, y, width, height],'String',defaults{k})
end
end
end
%Uicontrol for output object:
out = uicontrol('Parent',f,'Style','text','units','normalized',...
'position',[xo-deltaH,y+deltaV,width,height],'String','Val = ');
%Uicontrol for button execution:
exe = uicontrol('Parent',f,'Style','pushbutton','units','normalized',...
'position',[xo-2*deltaH,y+deltaV,width,height],'String','Calculate',...
'Callback',{#computations,ui_edits,out});
%Function executed by the "exe" uicontrol.
function computations(obj,~,ui_edits,out)
no_inputs = length(ui_edits);
summation = 0;
for i = 1:no_inputs
in = get(ui_edits{i},'String');
val = str2double(in);
summation = summation +val;
end
set(out,'String',['Val = ',num2str(summation)])
end
The above script produces:
Here's the gist:
The figure is a graphical object needed to contain the editable fields and text descriptions.
The fields are objects that enable user input (These are uicontrol objects).
The static texts establish to the user the correspondence between field and input (These are also uicontrol objects).
The button is an object that executes the callback function (also a uicontrol).
The callback function is a user-defined function that collects inputs from uicontrol objects and uses them to run code.
In this example, the "calculate" button simply sums up all the numeric values of the inputs. For your applications, you must modify the programmer-defined function at the end to suit your needs.
This kind of GUI lets users input information as strings. Said information must be converted from the string type to the double type. Other GUI objects allow for direct numerical inputs (such as sliders or buttons with underlying logical structure, ie. "if this button, then this value"). When working with editable fields, the programmer must be conscious that the inputs are strings until run through a function like str2double.
Other nuances with GUI's:
Every object that the programmer intends to be interactive must have an associated programmer-defined function.
In this case, the only interactive object is the button.
Personally, my biggest challenge when making MATLAB GUI's is dealing with the positioning of the UI controls. I have used a few for loops and cell arrays to generate the grid pattern for the texts and buttons. This is quality of life on my part, more elaborate GUI's may require more sophisticated coding.
MATLAB has a feature called "GUIDE" which helps automate the uicontrol generation. This is worth a try if the programatic method I have shared is too tedious.

Matlab Data Import from a text file

How do i import data of the following format.
1.0,X
.,-1.0
The size of the table may differ. As in the next file can also be a valid input file
.,.,.,1.0
.,X,.,-1.0
.,.,.,.
I have tried doing this using readtable to read the data and then use strcmp to assign the values to a different matrix but it doesnt work. What am i doing wrong.
Code:
foo = readtable(environment_file)
bar = zeros(size(foo,1),size(foo,2));
for i = 1:size(foo,1)
for j = 1:size(foo,2)
if strcmp(foo(i,j),'.')
bar(i,j) = 3;
elseif strcmp(foo(i,j),'X')
bar(i,j) = -10;
elseif strcmp(foo(i,j),'1.0')
bar(i,j) = 1;
elseif strcmp(foo(i,j),'-1.0')
bar(i,j) = -1;
end
end
end
bar
There are two problems in your code. One is that readtable reads the file as a table and takes the first row in the file as the names of the columns. So therefore, you should have the names at the top of the file.
Secondly, to access the contents of the table you need to use {} and not (). Also, there is no need to use for loops as you can use logical indexing to assign values to your matrix bar_b. Therefore, the changed code is as follows:
dataArray = readtable('environment_file.txt');
[n, m] = size(dataArray);
bar_b = zeros(n, m);
bar_b(strcmp(dataArray{:, :}, '.')) = 3;
bar_b(strcmp(dataArray{:, :}, 'X')) = -10;
bar_b(strcmp(dataArray{:, :}, '1.0')) = 1;
bar_b(strcmp(dataArray{:, :}, '-1.0')) = -1;

Set data tips programmatically?

I need to be able to set data tips programmatically from a list of array of x axis values. For example, I create a figure and plot my data.
figure;plot(t1,[filter(b,a,Gyro(:,2)),filter(b,a,Gyro(:,4))])
I have a set of timestamp values from t1 variable (time) (e.g. [0.450, 0.854, 1.2343....]) where I want to place data tips to mark certain events in my data. Without having to place them every time manual by clicking and saving data trip... How can I pass them as array and do this programmatically through matlab script?
You can add matlab datatip programatically and customize them to an extent.
The function below shows how to add a few datatip, position them and customize their display:
The code for this demo (save that in a file demo_datatip.m and run it to obtain the above figure) :
function h = demo_datatip
%// basic sample curve
npts = 600 ;
x = linspace(0,4*pi,npts) ;
y = sin(x) ;
%// plot
h.fig = figure ;
h.ax = axes ;
h.plot = plot(x,y) ;
%// simulate some event times
time_events = x([25 265 442]) ; %// events type 1 at index 25, 265 and 422
%// define the target line for the new datatip
hTarget = handle(h.plot);
%// Add the datatip array
h.dtip = add_datatips( time_events , hTarget ) ;
function hdtip = add_datatips( evt_times , hTarget )
%// retrieve the datacursor manager
cursorMode = datacursormode(gcf);
set(cursorMode, 'UpdateFcn',#customDatatipFunction, 'NewDataCursorOnClick',false);
xdata = get(hTarget,'XData') ;
ydata = get(hTarget,'YData') ;
%// add the datatip for each event
for idt = 1:numel(evt_times)
hdtip(idt) = cursorMode.createDatatip(hTarget) ;
set(hdtip(idt), 'MarkerSize',5, 'MarkerFaceColor','none', ...
'MarkerEdgeColor','r', 'Marker','o', 'HitTest','off');
%// move it into the right place
idx = find( xdata == evt_times(idt) ) ;%// find the index of the corresponding time
pos = [xdata(idx) , ydata(idx) ,1 ];
update(hdtip(idt), pos);
end
function output_txt = customDatatipFunction(~,evt)
pos = get(evt,'Position');
idx = get(evt,'DataIndex');
output_txt = { ...
'*** !! Event !! ***' , ...
['at Time : ' num2str(pos(1),4)] ...
['Value: ' , num2str(pos(2),8)] ...
['Data index: ',num2str(idx)] ...
};
If you need to delete a data tip, you can simply call delete(datatip_handle) on it's handle (or even an array of handles to delete them in group).
Here is a small example that might be what you are looking for:
time = 0:0.5:10;
values = rand(1,length(time));
datatip_index = [3 6]; % an 'event' occurs on the 3rd and 6th datapoint
datatip_text = {'event1', 'event2'};
figure;
plot(time,values)
hold on
text(time(datatip_index), values(datatip_index), datatip_text)

can I draw a line with different color by matlab in kml file format

I need to write line in kml file format with malab, this line is multiple color according to signal level or multiple line discontinuity?
by using 'kmlwriteline' it possible to write a line with one color, but I need to draw thie line with different color according to signal level or between to 3 color red,green,yellow
thank you .
There are 2 ways to do that. First one is, you can get the function here or kmlwrite()function and can use its color specification.
Second one is you can generate your one colour line and can open the kml file with Google Earth application then you can change the colour of line by right clicking on lines. The second one is manual way.
Edit: I see your point. You must create geoshape objects to perform your task briefly. If you want to segment the current line you must use NaN value.
Now try to code below:
% % // import your excel file
lat = xlsread('hossain1.xlsx','data','A2:A445');
lon = xlsread('hossain1.xlsx','data','B2:B445');
Rx_Level = xlsread('hossain1.xlsx','data','C2:C445');
% Initializing some stuff
Rx = [-60 -70 -80 -90 -100];
color = ['g', 'y', 'r', 'k']; % // I adjusted the colour wrt your uploaded pic.
width = 12 : -3 : 3; % // In the pic, Rx levels have a different witdh lines.
% // Existing Geographic shape object
lines = geoshape();
len = length(lat);
for u = 2 : length(Rx)
indexWrtRxLevel = ( Rx_Level >= Rx(u) ) & ( Rx_Level < Rx(u-1) );
lines(u-1).Latitude = zeros(1, len);
lines(u-1).Longitude = zeros(1, len);
indexOfZeros = find(indexWrtRxLevel == 0);
indexOfOnes = find(indexWrtRxLevel == 1);
lines(u-1).Latitude( indexOfZeros ) = NaN;
lines(u-1).Longitude( indexOfZeros ) = NaN;
lines(u-1).Latitude( indexOfOnes ) = lat( indexOfOnes );
lines(u-1).Longitude( indexOfOnes ) = lon( indexOfOnes );
lines(u-1).Name = ['>= ', num2str(Rx(u)), ' dBm'];
lines(u-1).Color = color(u-1);
lines(u-1).Width = width(u-1);
end
filename = 'Rx Levels';
kmlwrite(filename, lines, ...
'Name', lines.Name, ...
'Color', lines.Color, ...
'Width', lines.Width);
What I got is:
I hope that would be useful.

How do I format a MATLAB figure text annotation into rows and columns?

I have a script which fits some optical data to a sum of Lorentzian oscillators, and then spits out a figure with the original data and the fit. I would also like to include a text annotation with a table of the fitting parameters, but cannot figure out how to get rows and columns in my text box.
Each peak has 3 parameters and then there are 3 more global fitting parameters. My first try was to do this:
ParamTableLabels = {'\omega_p (cm^{-1})', '\omega_0 (cm^{-1})', '\Gamma (cm^{-1})'};
ParamTableVals = num2cell(Ef);
ParamTableLabels2 = {'d (\mu{m})','\epsilon_\infty','Scale'};
ParamTableVals2 = {ThickFit,EinfFit,ScaleFit};
ParamTable = vertcat(ParamTableLabels,ParamTableVals,ParamTableLabels2,ParamTableVals2);
where Ef is my 3xN matrix of fitting parameters. After generating my figure, I try to place the table in my plot at a suitable set of coordinates X,Y using:
text(X,Y,ParamTable)
and the result is a single column of text, no rows. My second attempt, which sort of works is to break up each column:
text(X, Y,ParamTable(:,1));
text(X+dX, Y,ParamTable(:,2));
text(X+2*dX,Y,ParamTable(:,3));
This almost works, but the subscripts in the labels throw off the vertical alignment of the last few rows, and it takes an undue amount of tinkering to get the spacing correct. I'm spending more time trying to get the text box to look right than to do the actual modelling.
How can I programatically format a block of text, containing both labels and variables, into rows and columns, and then use it as a text annotation in a figure with minimal user tinkering?
This is a not well supported using basic commands. But you can at least save yourself the trouble of guessing the subsequent X positions by making Matlab do the work for you.
The key is the "Extent" read-only parameter attached to a text block. Use docsearch text properties to see the documentation.
Putting this into some code:
padFraction = 0.1; %This is roughly the unitless padding between columns, as a fraction of the column on the left.
curX = X; %Leave the initial X variable unchanged
%For each text block column, add the text block, get the extent, and adjust curX
h = text(curX, Y,ParamTable(:,1));
curExtent = get(h, 'Extent');
curX = curExtent(1) + curExtent(3)*(1+padFraction);
h = text(curX, Y,ParamTable(:,2));
curExtent = get(h, 'Extent');
curX = curExtent(1) + curExtent(3)*(1+padFraction);
text(curX,Y,ParamTable(:,3));
The full script used to generate/test is below:
ParamTableLabels = {'\omega_p (cm^{-1})', '\omega_0 (cm^{-1})', '\Gamma (cm^{-1})'};
Ef = round(rand(10,3)*100);
ParamTableVals = num2cell(Ef);
ParamTableLabels2 = {'d (\mu{m})','\epsilon_\infty','Scale'};
ParamTableVals2 = {'ThickFit','EinfFit','ScaleFit'};
ParamTable = vertcat(ParamTableLabels,ParamTableVals,ParamTableLabels2,ParamTableVals2);
X = 1; Y = 1.1;
%Put something in the plot
figure(1); clf; hold on;
plot(-10:10, randn(21,1)*20,'.');
codeblock = 3;
switch codeblock
case 1
text(X,Y,ParamTable)
case 2
dX = 3;
text(X, Y,ParamTable(:,1));
text(X+dX, Y,ParamTable(:,2));
text(X+2*dX,Y,ParamTable(:,3));
case 3
padFraction = 0.1;
curX = X;
h = text(curX, Y,ParamTable(:,1));
curExtent = get(h, 'Extent');
curX = curExtent(1) + curExtent(3)*(1+padFraction);
h = text(curX, Y,ParamTable(:,2));
curExtent = get(h, 'Extent');
curX = curExtent(1) + curExtent(3)*(1+padFraction);
text(curX,Y,ParamTable(:,3));
end