Avoid repetition when selecting two columns of same table - matlab

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'})

Related

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.

Set column default value in Google Sheets

I have a finance tracking worksheet that has a number of formulas for each row. What I'd like to add is have these formulas replicate whenever a new row is inserted (fill it with a "default value").
Sheet before adding the row:
Sheet after adding the row:
I'd like some of these cells (B28 in this case) to contain a formula. for this row the formula is:
=IF(ISBLANK(C27), "", CONCATENATE(YEAR(C27), "-", MONTH(C27)))
You might want to consider an ArrayFormula. Here's a place for getting detail. In your case, the big trick is knowing that you can run the formula through all of Column C using the notation something like C1:C. Doing it this way is far more self documenting than burying it in a Google function and it's way more general.
Because the formula is the same for all rows, I added a function that just does the following:
function addRow() {
SpreadsheetApp.getActiveSheet().getRange('B7:B').setValue('=IF(ISBLANK(C7), "", CONCATENATE(YEAR(C7), "-", MONTH(C7)))');
}
I set this function to run on a trigger whenever the sheet is edited. The row numbers increment themselves so not problem there either.
Used this SO question for help: Default cell values when Inserting new row in Google Sheets
delete everything in your B column and paste this into B1 cell:
={"Date", ARRAYFORMULA(IF(C2:C<>"", YEAR(C2:C)&"-"&MONTH(C2:C)))}

How to replace crosstab cell's value with a string

I have column fact, it can carry some difference values:
- Positive values - real values, need to be outputted as is
- 0 is null, output as is
- -1 - special value. Need to ouput "VAC" string in cell.
- -2 - special value. Need to output "SICK" string in cell.
I tried to do it with editing dimension, i replace it with:
case
when [BaseEmp].[FACT_VALUE] = -1 then 'VAC'
when [BaseEmp].[FACT_VALUE] = -2 then 'SICK'
else to_char([BaseEmp].[FACT_VALUE])
end
But now I see error: ORA-01722 invalid number (i think, because strings cannot be aggregated). In column properties I select "min" as aggregate function.
How to replace my special values with strings?
You don't need to change value to VAC, SICK etc.
You need to change DISPLAYED value.
Unlock the padlock in RS.
Select text in your cell
Set text source to "Report Expression"
Write expression like
CASE
WHEN cellValue() = -1 THEN 'VAC'
WHEN cellValue() = -2 THEN 'SICK'
WHEN cellValue() = 0 THEN ''
ELSE cellValue()
END
I tried thinking of a work around regarding your problem. I'm not really sure what you crosstab looks like but considering your parameters above, try creating a data item which holds your case condition for your fact.
Ex.
case
when [BaseEmp].[FACT_VALUE] = -1 then 'VAC' <--- this will produce Any Error Characters
when [BaseEmp].[FACT_VALUE] = -2 then to_char([BaseEmp].[FACT_VALUE]/0) <--- this will produce Missing Value Characters
else to_char([BaseEmp].[FACT_VALUE]) <---- as is
end
Then set the data item's Data Format to number and set the values for the properties as follows:
Kindly give this a try and hopefully it can somehow resolve you problem. Cheers!
Nikita Kuhta checkout Alexey's Baturin's answer. I think it's the appropriate approach. Just one thing if you unlock and select the text in the cell, all of the cell values will be selected and affected by the report expression. You might have several columns or other fact items in the crosstab. If that's the case, what you can do is
Unlock the Report.
Select the crosstab cell/intersection you want to change.
Set the Define Content to Yes.
Drag a Layout Calculation in the crosstab cell/intersection, then insert your case statement.
Thanks to a friend who told be about the define contents. :D

Getting selected cell indices from uitable

I have a uitable with 10 columns, which I'm populating from a db.
Now I want to know when the user choose a specific row. For example if the user chooses the 3rd record, I would like to get back the value 3, so then I could access the actual information to for example open that specific record from the path.
I found online that I would need findjobj.
I also think that the method should be implemented in here:
function uitable_CellSelectionCallback(hObject, eventdata, handles)
However I found a little information about how I should proceed.
Anyone had this problem or knows how to solve it?
When calling the CellSelectionCallback you can access the Indices property, which is a 2 x 1 array containing the row and column indices of the cell you have selected.
Therefore in your callback, use something like this:
row = eventdata.Indices(1)
col = eventdata.Indices(2)
and that should get you going.
In order to avoid callbacks, you can use app.UITable.Selection in the newer versions of MATLAB where app is your app object and UITable your uitable name

Tables to word from 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.