Tables to word from Matlab - matlab

My code in Matlab, after calculation, prints out the result in word. I am using writetoword.m for this, and my results are mostly in terms of tables. I need help in the alignment of these tables.
FileSpec = fullfile(CurDir,[WordFileName,'.pdf']);
[ActXWord,WordHandle]=StartWord(FileSpec);
WordCreateTable(ActXWord,NoRows,NoCols,readings,6);
function WordCreateTable(actx_word_p,nr_rows_p,nr_cols_p,data_cell_p,enter_p)
if(enter_p(1))
actx_word_p.Selection.ParagraphFormat.Alignment=1;
actx_word_p.Selection.TypeParagraph;
end
actx_word_p.Selection.ParagraphFormat.Alignment=1;
actx_word_p.ActiveDocument.Tables.Add(actx_word_p.Selection.Range,nr_rows_p,nr_cols_p,2,1);
for r=1:nr_rows_p
for c=1:nr_cols_p
actx_word_p.Selection.ParagraphFormat.Alignment=1;
WordText(actx_word_p,data_cell_p{r,c},'Normal',[0,0]);
if(r*c==nr_rows_p*nr_cols_p)
actx_word_p.Selection.MoveDown;
else
actx_word_p.Selection.MoveRight;
end
end
end
end
function WordText(actx_word_p,text_p,style_p,enters_p,color_p)
if(enters_p(1))
actx_word_p.Selection.TypeParagraph;
end
actx_word_p.Selection.Style = style_p;
if(nargin == 5)
actx_word_p.Selection.Font.Color=color_p;
end
actx_word_p.Selection.TypeText(text_p);
actx_word_p.Selection.Font.Color='wdColorAutomatic';
for k=1:enters_p(2)
actx_word_p.Selection.TypeParagraph;
end
set(actx_word_p.Selection.ParagraphFormat,'Alignment',1);
end
I want to print all the tables in a single sheet, and I need tables side by side. With the above code it's always starting in a new line. How can I do this?

When I want two tables side by side in Word I usually start with a new "assisting" table that has the following particulars
1 row, 2 columns
full page width
no borders
centered text
Then I write the first table into the left cell of the "assisting" table and the second into the right cell. I also managed to do that from MatLab with a COM-server as you're using it. Some coding is needed but nothing too fancy. If you have questions just let me know.

Related

Link editable field to a certain cell in table figure in MATLAB app designer

I am building an application in MATLAB app designer (2019b), and I am trying to link two blank fields to a table that has only two columns, such that the first field should show the first value (in the first column) in the table and the other should show the last value in the first column in the table.
Example
table:
9 2
3 4
5 6
blank field_1: 9
blank field_2: 5
I am a C++ person, so whenever I am developing, for instance in SFML, I just have one event loop that captures and updates everything - no matter where I press on the window, but, in MATLAB, whenever I press a button - I need to build a separate callback function. Here, I am not calling back anything - I just need to update the value.
Anyone, please help?
Thank you
Here's an example which illustrates how you can achieve the behavior you want.
In the below example, I am creating a uitable in the App startup, and I am defining a callback which updates the table as required when the cells are modified.
function startupFcn(app)
vars = {9,2;3,4;5,6;'blank _field_1:', '';'blank field_2:', ''};
t = uitable(app.UIFigure,'Data',vars);
t.ColumnEditable = true;
t.DisplayDataChangedFcn = #updateTable;
function updateTable(src,~)
src.Data(end-1:end,2:end) = [src.Data(1,1) src.Data(end-2,1)].';
end
end

LibreOffice macro showing simple TextBox shape

I cannot figure out (or find an example) how to perform the following simple thing in the LibreOffice Calc 6.2:
I have a drawing shape (e.g. a simple rectangle) in a sheet (call it ShapeA) and a textbox shape in another sheet (call it TextboxB). I want to do the following: when I click on the ShapeA, the TextboxB must appear on the screen (without changing the current sheet, maybe in a dialog box) and then be closed with a mouse click.
I guess the macro associated with ShapeA could look something like this:
Sub Main
oDrawPage = ThisComponent.getDrawPage()
oTb = oDrawPage.getByName("TextBoxB")
oTb.show()
End Sub
Could someone advise what I should put into this macro to accomplish the described task?
UPDATE: What I want to accomplish ( in reply to Jim K.).
I have a very cluttered diagram with many shapes. Each shape has some textual information associated with it. There is not enough space on each shape or around it to contain this info. So there is must be a way to display this info about each shape. Also this information should be displayed in a preformatted way (it contains code and other structured info).
My plan is to create a textbox with the relevant information for each diagram shape, place these textboxes in other sheet and have a possibility, when viewing diagram, to click on any shape and view the associated info in the poped up textbox without leaving the diagram, and then close the textbox with a simple action (e.g. by clicking on it).
Does this task sound feasible to be realized with the LO's shapes and macros?
How about this: Put everything on the same sheet but keep the text boxes hidden until needed.
Use the following code adapted from https://ask.libreoffice.org/en/question/93050/how-can-i-hideshow-a-shape-of-my-spreadsheet-using-a-macro/.
Sub ShapeClickedA
ShowHideShape("TextBoxA")
End Sub
Sub ShapeClickedB
ShowHideShape("TextBoxB")
End Sub
Sub ShowHideShape(shapeName As String)
oDrawPage = ThisComponent.getSheets().getByName("Sheet1").getDrawPage()
For iShape = 0 To oDrawPage.Count - 1
oShape = oDrawPage.getByIndex(iShape)
If oShape.Name = shapeName Then
If oShape.Visible Then
oShape.Visible = 0 'Not Visible
Else
oShape.Visible = 1 'Visible
End If
End If
Next iShape
End Sub
If you haven't yet, set the names of the text boxes by right-clicking and choosing Name... Then right click on both ShapeA and TextBoxA and assign the macro ShapeClickedA. Do likewise for other pairs of shapes. The result works like this:
Before anything is clicked.
Click on ShapeA. (To close it again, click on either ShapeA or TextBoxA). ShapeB functions similarly.
It's also possible to display both at the same time.

How to display table in parts in command window in matlab?

I have a table which consists of 17025 rows. When I try to display the table, the whole table displays. But I want it in small parts. How do I make it display in small parts.
Type more on at the MATLAB command prompt. Subsequent output will be paused after every screenful.
Documentation: https://www.mathworks.com/help/matlab/ref/more.html
I usually check my variables in these ways:
Suppose that we have a variable called A=rand(1000,1000).
1.we can display a part of A by calling A(127:130,241:243), to show the specific part of it.
>> A(127:130,241:243)
ans =
0.8152 0.0674 0.5609
0.1977 0.3906 0.6491
0.3288 0.1255 0.7478
0.9176 0.4253 0.5111
2.double click the name of the variable in the Workspace, so we could check it in Variables.
3.if your data only contain few columns, I recommend you to draw them in a figure and view them by dragging a Data Cursor in the picture.
SampleCode:
A=[1:1:200; 250:-1:51];
plot(A')
However, only the first option could display matrix on command window, but 2&3 is much faster since we don't know which part to display.

LibreOffice BASIC function - Copy if over a value to a new sheet

I'm trying to create a simple function on a LibreOffice Calc sheet.
I have a table of information where cells A1:K2 has headings (the cells in Row 2 are merged)
all the information runs from A3:K176
The sheet has various functions running on it already, all I need is one more function.
At first I wanted it to automate when i open, but then i thought running the BASIC macro off of a button might be better, so i can edit and then just hit the button once I'm done.
I have the button linked up to run the macro already, but now im stuck... I have tried many ways of writing the code, but it seems that i am just too green to figure it out.
the last time i ran Excel advanced functions i was in high school.
ok, enough talking...
All I need the macro to do is once I hit the button, I need it to check column K and any value > 1 it needs to take that whole row and copy it then go to a target sheet, clear any data on the target sheet. then copy that set of rows to the next open row in the target sheet. possibly even exporting the result to a .txt file as a bonus.
this is a start...
in future I would like to add another button that will do the same function to a array lower down and put that into a different sheet.
Option Compatible
Sub SWD_AwakeningsTrade
Dim i, LastRow
LastRow = Sheets("Card_List").Range("A" & Rows.Count).End(xlUp).Row
Sheets("Awakenings For trade").range("A2:I500").clearContents
For i=2 to LastRow
If Sheets("Card_List").Cells(i,"K").Value = "(>1)" Then
Sheets("Card_List").Cells(i, "K").EntireRow.Copy
Destination:=Sheets("Awakenings For trade").Range("A" & Rows.Count.end(xlUp)
End if
next i
End Sub

Avoid repetition when selecting two columns of same table

Each time I would like to work on two variables from the same table, I repeat the table's name for selecting them like in the following example:
boxplot(hospital.Weight, hospital.Sex)
This is a problem if the table's name changes because it requires two further changes to get it right.
Is there a more elegant way to avoid the repetition in the function call?
I tried:
boxplot(hospital( : , {'Weight', 'Sex'}))
But this returns the columns as tables and boxplot as far as I understand only takes vectors.
What I would do is create a dummy variable before generating your box plot so that you'd only have to change one line:
table_name = hospital; %this is the line you'd change if the table name changes
boxplot(table_name.Weight, table_name.Sex)
The answer by #qbzenker is probably the best approach, but if you look for somthing else, you can use cell arrays for this:
C = table2cell(hospital(:,{'Weight','Sex'})); % this is the only place to change the table name
boxplot([C{:,1}],[C{:,2}])
If you want this in a nicer syntax, you can write a small function like:
function my_BoxPlot(T,F)
C = table2cell(T(:,F));
boxplot([C{:,1}],[C{:,2}])
end
and then call it with your preferred syntax:
subplot 121
my_BoxPlot(hospital,{'Weight','Sex'})
subplot 122
my_BoxPlot(hospital,{'Age','Sex'})