How to create a table from another table in Matlab (version r2021b)? - variables

I have a mat. file called 'settings' which contains an S file and a T file, which is a Table. From this T file, which is a table nested in the mat. file, I would like to create another table, e.g., T1, and include only certain variables from the original T table by adding or removing variables.
What I did is the following:
Settings = load('settings_20211221.mat'); %load data file and its subcontituents because the table T where the data are is nested in the settings_mat. file
S = Settings.S;
T = Settings.T;
I see that Matlab has accepted that T is a table because I can see the size(T) or head(T). However, it is proving very hard to continue and create my own table afterwards.
1)
T1 = readtable('T')
Error using readtable (line 498) Unable to find or open 'T'. Check the path and filename
or file permissions.
Question 1: I do not understand why it could be that I cannot read table T unless it has to do with the fact that is nested and I am missing something? My impression was that I have specified the Table and that I could thus apply the readtable function to it.
After this error code, I decided to simply create a duplicate of the T1 table called 'Table' in case for some reason I had no permission to manipulate the original T. I want to remove lots of variables from the table and I figured the easiest thing to do would be to specify the ranges of variables corresponding to the columns I want to remove.
Removing variables from the newly created table 'Table'.
T1 = removevars(T1, (2:8)) %specifying one range between 2 and 8 works
Table = removevars(Table, [24 25 26]) %using a numeric array to indicate the individuals positions of the variables I want to remove works
Then I wanted to specify the ranges all in one go either by using () or [] to be more efficient and did the following:
Table = removevars(Table, (25:28), (30-62))
Table = removevars(Table,[25:28], [30-62])
I always got the following error 'Error using tabular/removevars. Too many input arguments.'
Question 2: How can I specify multiple ranges of numbers corresponding to the table columns/variables I want to remove?
Alternatively, I thought I could specify the variables I want to remove using strings but I got the below error message even though both the 'flip_angle' and 'SubID' columns did exist in my table.
Table = removevars(Table,{'flip_angle', 'SubID'})
Error using tabular/subsasgnParens (line 230)
Unrecognized table variable name 'flip_angle
Sometimes I tried to specify multiple strings corresponding to the names of the variables I wanted to remove (e.g., 20 strings), and then Matlab would return an error for 'too many input arguments'.
Question 3: How are variabes removed using strings?
Question 4: Is there a more efficient way to create a new table from the original T file by indexing the variables I want to include in some other way?
I want to understand why I get these error codes, so any help would be much appreciated!
Thank you!

Related

How to get the column index number of a specific field name in a staged file on Snowflake?

I need to get the column number of a staged file on Snowflake.
The main idea behind it, is that I need to automate getting this field in other queries rather than using t.$3 whereas 3 is the position of the field, that might be changed because we are having an expandable surveys (more or less questions depending on the situation).
So what I need is something like that:
SELECT COL_NUMBER FROM #my_stage/myfile.csv WHERE value = 'my_column_name`
-- Without any file format to read the header
And then this COL_NUMBER could be user as t.$"+COL_NUMBER+" inside merge queries.

How to put the values of a column of an internal table into a variant?

Can somebody help me figure out if there is a way for the query below:
I have an internal table with one column with 69 records.
I want all these 69 records to be populated into a variant and get saved so that with this variant and the values saved in it I can run a particular program
How can I populate these values ?
Your question is a bit unclear for me.
Do you speak about two programs or one program?
What's the parameter you want to fill in the variant?
I'll just give you some hints - depending on your situation you must pick the correct parts.
I have an internal table with one column with 69 records.
How is the internal table filled?
I want all these 69 records to be populated into a variant and get saved so that with this variant and the values saved in it I can run a particular program
You have a program and you want to save a selection in a variant. So you need some parameters for the selection screen.
You want a table, so you need a SELECT-OPTION.
To define a SELECT-OPTION you need a DDIC-reference (you must say, what kind of field you want.). In the following example I use a material number (MARA-MATNR).
So you program contains something like:
TABLES mara.
SELECT-OPTIONS: s_matnr FOR mara-matnr.
With this you would get:
You can define ranges (from-to) and list of values. As you want only single values, you need something like:
SELECT-OPTIONS: s_matnr FOR mara-matnr NO INTERVALS.
Now you get:
When you push (1) you can enter values.
With (2) you can load from an external file,
with (3) you can load values from clipboard.
So you can fill your values and store the selection in a variant.
When you execute your program, the data is stored in a ranges table:
Now you can loop on this table and copy the S_MATNR-LOW value into your internal table for further processing.
If I misunderstood you question and you want to create a variant dynamically, then take a look on function module RS_VARIANT_ADD (or RS_VARIANT_COPY,RS_VARIANT_CHANGE...)
You could always put the values in TVARVC, either manually or via code. Then specify the TVARVC variable in the variant definition.

How do I insert a table into a cell using word vba or a table within a table?

I am new on VBA, I am creating a script to generate a report from a DB, I have been able to assemble a general draft of my report but I need to insert a table into an existing cell inside a word document, I have been surfing around but I am unable either to do a websearch with the correct terms in order to find some guidance on how to achieve this, If I am able to do it with my mouse I am sure I am able to do it through scripting, any resources that would help me in the right direction will be deeply appreciated.
Ok I found a way, The secret is in the Range, to specify where do you want your nested table to be placed.
so, I am using Powershell so the syntax might vary a little
For creating the Table in the Document (assuming you already have a created document, if not you are missing that part which I am not going through since they already are several question/answer pairs on that subject).
$TableX = $oDoc.Tables.Add($oDoc.Bookmarks("TableX").Range, 4, 3)
So TableX is our actual table, then we are telling word to use the helper method on the oDoc (Which is the name of our document object) to add a table, with a Bookmark named TableX (I will again not do a large explanation on this one, just for practical purposes, we name the Bookmark so we can reference to the table by Bookmark name later if we need to add data to it or manipulate it in any way we need) and at the same time we are calling to the method Range which is going to tell the Document Object where do we want the table to be placed, since we have not defined the range explicitly it will insert it in the next available line on the document,
Finally we specify how many rows and columns we would like in the table.
That is what we need for creating a table, now the tricky part, how do we insert a nested table, and moreover how do we specify where do we want this second table to be nested.
Well, with this:
$oNestedTable = $oDoc.Tables.Add($TableX.Cell(4, 2).Range,7,3)
We name our nested table oNestedTable, then we are calling the same helper method we called before to add a new table to the document, but wait, look carefully at the differences, the range part of the command is pointing to a specific cell on in our first table, that is in the fourth row within the second cell, it is there where we are explicitly telling the document to insert a new table with 7 rows and 3 columns.
I hope this gives you some bare minimum guidance.
Regards
en

How to make load script that load multiple files in to one table

I have multiple files named by convention: "data_YYYY.MM.xslx"
I need to load all these files to one qlikview table, but when I do:
Tab:
load Name, Number from [data_*.csv];
I get Tab, Tab-1, Tab-2 files for each file.
I`ve also tryed to do:
Tab:
add load Name, Number from [data_*.csv];
With the same effect.
If anybody know the way how to do it, please help.
This question makes no sense, unless you have omitted some detail.
QlikView will implicitly append all data to the original table 'Tab' by a statement such as:
Tab:
load Name, Number from [data_*.csv] (txt);
Note the file format specified in brackets.
Appending data implicitly occurs when ever a table is loaded with exactly the same field names as a table already created. So in your example, the first file encountered constitutes a load of data from that file. Assuming the field names are indeed referenced as per your question, the resultant table should have two fields in it: 'Name' and 'Number'. When the second file is encountered via the wildcard match, the second load takes place and it will append that data to the table 'Tab'.
If you wish to NOT rely upon IMPLICIT concatenation (QlikView terminology for appending data to an existing table) then you can write a FOR loop to load your files instead whist using the explicit CONCATENATE load prefix to point to the table you wish to append data to.
E.g.
// QV trick to 'declare' a table
Tab:
LOAD null() AS Name
AUTOGENERATE(1)
WHERE RecNo() < 0;
FOR EACH file IN filelist('data_*.csv')
CONCATENATE('Tab')
LOAD * FROM [$(file)] (txt);
NEXT file
This hack works for me:
tmp:
LOAD #1 inline [#1];
Tab:
Concatenate load Name, Number from [data_*.csv];
You can do it by this way:
Load * from data_*.csv;
Simply use a mask in the file name.
And for completeness one way with a loop (here using qvd files):
FOR Each db_schema in '2013-07','2013-08','2013-09'
LOAD ...., db_schema
FROM `x-$(db_schema)`.`qvd` (qvd);
next;
(Knowing the names.)
Here is another way if the files names are different
Tab:
load Name, Number from [data_1.csv];
join (Tab)
load Name, Number from [data_2.csv];

SSIS external metadata column needs to be removed

I am creating a select statement on the fly because the column names and table name can change, but they all need to go into the same data destination. There are other commonalities that make this viable, if I need to later I will go into them. So, what it comes down to is this: I am creating the select statement with 16 columns, there will always be sixteen columns, no more, no less, the column names can change and the table name can change. When I execute the package the select statement gets built just fine but when the Data Flow tries to execute, I get the following error:
The "external metadata column "ColumnName" (79)" needs to be removed from the external metadata column collection.
The actual SQL Statement being generated is:
select 0 as ColumnName, Column88 as CN1, 0 as CN2, 0 as CN3, 0 as CN4,
0 as CN5, 0 as CN6, 0 as CN7, 0 as CN8, 0 as CN9, 0 as CN10,
0 as CN11, 0 as CN12, 0 as CN13, 0 as CN14, 0 as CN15 from Table3
The column 'Column88' is generated dynamicly and so is the table name. If source columns exist for the other ' as CNx' columns, they will appear the same way (Column88 as CN1, Column89 as CN2, Column90 as CN3, etc.) and the table name will always be in the form: Tablex where x is an integer.
Could anyone please help me out with what is wrong and how to fix it?
You're in kind of deep here. You should just take it as read that you can't change the apparent column names or types. The names and types of the input columns become the names and types of the metadata flowing down from the source. If you change those, then everything that depends on them must fail.
The solution is to arrange for these to be stable, perhaps by using column aliases and casts. For one table:
SELECT COLNV, COLINT FROM TABLE1
for another
SELECT CAST(COLV AS NVARCHAR(50)) AS COLNV, CAST(COLSMALL AS INTEGER) AS COLINT FROM TABLE2
Give that a try and see if it works out for you. You just really can't change the metadata without fixing up the entire remainder of the package.
I had the same issue here when I had to remove a column from my stored procedure (which spits out to a temp table) in SQL and add two columns. To resolve the issue, I had to go through each part of my SSIS package from beginning (source - in my case, pulls from a temporary table), all the way through to your destination (in my case a flat file connection to a flat file csv). I had to re-do all the mappings along the way and I watched for errors that game up in the GUI data flow tasks in SSIS.
This error did come up for me in the form of a red X with a circle around it, I hovered over and it mentioned the metadata thing...I double clicked on it and it warned me that one of my columns didn't exist anymore and wanted to know if I wanted to delete it. I did delete it, but I can tell you that this error has more to do with SSIS telling you that your mappings are off and you need to go through each part of your SSIS package to make sure everything is all mapped out correctly.
How about using a view in front of the table. and calling the view as the SSIS source. that way, you can map the the columns as necessary, and use ISNULL or COALESCE functions to keep consistent column patterns.