Matlab cells with Names - string

Note: Please if you can point a solution without 'eval', that would be great!!!
If not, I'll be thankful too :-)
Well, I have a cell (Var_s) that has in the first row strings and in the second row matrices:
clc
clearvars
fclose all
L=[11 22 33 44];
M=[1 2 3];
N=[101 102 103 104 105, 95 96 97 98 99];
Var_s=cell(2,3);
Var_s(1,1:3)={'Rn', 'LE', 'O'}; %// The strings are all different and were not created in a loop.
Var_s(2,1:3)={L, M, N}; %// No correlation is possible.
%//Than I delete all variables because I can work with the cell (Var_s{2,:})
%//to execute some computations
clearvars L M N
%//Now I want to save the values which are stored inside of the cells in the
%//second line of Var_s, Var_s{2,:}, associating to them the names in the first
%//row of the Var_s, Var_s{1,:}, in a .mat file
%//But let's imagine that instead of having size(Var_s{2,:})=3 I would have
%//something like 1000 (but lets keep it simple for now having just 3).
%//Consequently I want to use a 'for' to do this work!
%//And it is at this point that the issue appears. How can I use the strings
%//inside Var_s{1,:} to create variables and store them in the .mat file with
%//the values in the cells of Var_s{2,:} associated to them?
filename='Clima';
a=0;
save(filename,'a'); %//Creats the file with a variable a=0
for i=1:length(Var_s(2,:))
genvarname(Var_s{1,i})=Var_s{2,i}; %//The idea is to create a variable using a stringn and associate the values
save(filename,char(Var_s{1,i}),'-append'); %//The idea is to add the created variable that has the same name in Var_s{1,i}
end
clearvars
%//After all this I could access the variables that are stored in 'Clima.mat'
%//by their name such as
load('Clima.mat')
Rn
LE
O
And the result must be
Rn = 11 22 33 44
LE = 1 2 3
N = 101 102 103 104 105

Your question is pretty much fully covered in the docs to the save() command under "Save Structure Fields as Individual Variables". To get there, you only must create that struct.
To create that struct(), where you dynamically create its field names, not much of your code must be changed. Once your struct is created in that loop, just save the struct once after the loop with the option '-struct', which automatically then generates a new variable for each field in that struct.
s = struct();
for i=1:length(Var_s(2,:))
s.(Var_s{1,i})=Var_s{2,i}; % struct with dynamic field names
end
save(filename, '-struct', 's');
Now let's see, what we stored:
whos('-file', 'Clima.mat')
Name Size Bytes Class Attributes
LE 1x3 24 double
O 1x10 80 double
Rn 1x4 32 double
As you can see, we stored 3 variables in that file.

Related

To extract street number from street address using regex from a dataframe in python

d1 = dataset['End station'].head(20)
for x in d1:
x = re.compile("[0-9]{5}")
print(d1)
Using dataset['End_Station'] = dataset['End station'].map(lambda x: re.compile("([0-9]{5})").search(x).group())
shows - TypeError: expected string or bytes-like object.
I am new to data analysis, can't think of any other methods
Pandas has its own methods concerning Regex, so the "more pandasonic" way
to write code is just to use them, instead of native re methods.
Consider such example of source data:
End station
0 4055 Johnson Street, Chicago
1 203 Mayflower Avenue, Columbus
To find the street No in the above addresses, run:
df['End station'].str.extract(r'(?P<StreetNo>\d{,5})')
and you will get:
StreetNo
0 4055
1 203
Note also that the street No may be shorter than 5 digits, but you attempt
to match a sequence of just 5 digits.
Another weird point in your code: Why do you compile a regex in a loop
and then make no use of them?
Edit
After a more thorough look at your code I have a couple of additional remarks.
When you write:
for x in df:
...
then the loop iterates actually over column names (not rows).
Another weird point in your code is that x variable, used initially to hold
a column name, you use again to save a compiled regex there.
It is a bad habbit. Variables should be used to hold one clearly
defined object in each of them.
And as far as iteration over rows is concerned, you can use e.g.
for idx, row in df.iterrows():
...
But note that iterrows returns pairs composed of:
index of the current row,
the row itself.
Then (in the loop) you will probably refer to individual columns of this row.

Easytrieve - Removing leading zero in numeric variable

Using Easytrieve program am trying to fetch the details from DB2. But one off the column is having integer datatype and am moving the data to binary variable.
And I need to perform arithmetic operation in this variable and result will be move to output file.
When i try to move binary value to alpha numeric am getting leading zero.
Not sure how to remove those leading value.
For example :
DB2 column name (Period_value) = 240
Easytrieve :
FILE TEMP1
TEMP-PERIOD 1 5 A
WS-VAL1 W 4 B 0
WS-TEMP W 4 N
....
....
using cursor to get the value from Db2
WS-VAL1 is holding the value 240
DISPLAY WS-VAL1 => OUTPUT : 240
* CALCULATION
WS-VAL1 = WS-VAL1 + 1
WS-TEMP = WS-VAL1
DISPLAY WS-TEMP -> OUTPUT : 0241
MOVE WS-TEMP to TEMP-PERIOD
PUT TEMP1
But output file contains
0241
Expected output
241
242 (without leading zero)
Could someone help me on this.

Split text and number, then convert and add numbers

I have a series of values such as:
10RP
2.5R
5R
7.5R
10R
2.5YR
5YR
I want to convert the string portion to a number based on this table:
0 R
10 YR
20 Y
30 GY
40 G
50 BG
60 B
70 PB
80 P
90 RP
I then want to create two columns so that:
2.5YR
becomes:
2.5 10
In a third column I will add the two numbers together.
Can this be done just using formulas? I want to avoid using VBA if I can.
Thanks.
Here's another approach.
seq is a defined name referring to an array constant ={1,2,3,4,5}
If you might have numbers that encompass more than five characters, just extend the constant appropriately.
Number part: =LOOKUP(9E+307,--MID(A1,1,seq))
Letter portion converted to number:
=VLOOKUP(MID(A1,LOOKUP(2,1/ISNUMBER(-MID(A1,1,seq)),seq)+1,9),$F$1:$G$10,2,FALSE)
Where your table is in F1:G10 and reversed so that the letters are in the first column
This might not be the most efficient but should work
=IFERROR((LEFT(F3,LEN(F3)-2)+VLOOKUP(RIGHT(F3,2),$A$3:$B$12,2,0)),
(LEFT(F3,LEN(F3)-1)+VLOOKUP(RIGHT(F3,1),$A$3:$B$12,2,0)))
Where your lookup table is A3:B12 with the letters in the left-most column
Check for the two-letter combinations before the single-letter ones.

Need to transpose a time series in Excel into a format readable by Stata

The format of the data is this.
Obs1 Var1 var1ObsPeriod1 var1obsPeriod2
Obs1 var2 var2ObsPeriod1 var2obsPeriod2
..
Obs2 var96 var96obs
...
and so on.
I need to transform this into
obs var1 var2 var96
obs1 var1obs var2obs... var96obs
obs1Period2 var1obsPer2 var2obsPer2 ....
obs2 var1obs....
It's a fairly simple operation in C# (the only language I know decently); however I'm unable to figure out how to do it with the tools accorded (Excel/VBA).
Can anybody help with this? It's a fairly simple operation in C#, where all I would need to do is apply a couple of loops and transform this into a 2d array with a row for each obs and its specific time period. However, I do not know how to do this in excel/vba.
I get that this is a big task, but can anybody point me towards how to set this up? (Can I use a 2d string or is there an equivalent...and so on.)
Img link: http://imgur.com/teXdo9x
Here's a more concrete example of what I need: the part at the top is how the data currently is; the part at the bottom is what I need it to become.
Assuming you have your initial data in Stata (see help import), then one way is:
clear
set more off
*----- example data -----
input ///
obs str1 vartitle period1 period2
1 "x" 25 45
1 "y" 67 89
2 "x" 56 23
2 "y" 98 34
end
order vartitle obs
sort vartitle obs
list
*----- what you want -----
// stack
stack period1 obs vartitle period2 obs vartitle, ///
into(metric obs2 vartitle2) clear
isid vartitle2 obs2 _stack, sort
rename _stack period
// reshape
by vartitle2: gen obs = _n
reshape wide metric, i(obs) j(vartitle2) string
rename metric* *
// clean
order obs obs2 period
list
The variable obs can be thought of as a unique identifier for each observation. obs2 can be thought of as a panel identifier.
Key are help stack and help reshape. Use list to check what's going on with the database in between commands.

is it possible to index character strings pulled from an external .txt file?

I'm in a basic MATLAB college course, and need some help with my code.
theres an external .txt file with names in it, with corresponding numbers assigned to each name. my goal is to place all the first names, last names, and numbers into arrays, find the lowest number in the 'number' array, get the corresponding indexer number, and print the first and last name related with that number.
the text file reads 25 different names and numbers
(i.e.:
Bob
Smith
17
Jane
Doe
23
Bill
Johnson
13
...etc...)
here is my general code so far:
1 clear
2
3 clc
4
5
6 fid1=fopen('facedata.txt','rt');
7
8 for index = 1:1:25
9 firstn(index) = fgetl(fid1);
10 lastn(index) = fgetl(fid1);
11 number(index) = fscanf(fid1,'%f');
12 end
13
14 [distmin,I] = min(dist);
15 fprintf('%5.4f %10s %10.0f', distmin, firstn(I), I);
My hope is for the code to run through, get matlab to recognize '13' as the lowest number, and print 'bill johnson' to the screen, but if I run the code, matlab says there are errors
Subscripted assignment dimension mismatch.' # line 9.
and
Index exceeds matrix dimensions.' # the firstn**(I)** in line 15.
any ideas?? i know this is crazy long, but any help would be appreciated! :]
The command fgetl means read a line from the text file. Therefore your code is reading 2x25 = 50 lines of text. How do you know that your file has this many lines in it? You should read a new line, process it, and repeat until you reach the end of the file:
fid = fopen('fgetl.m');
tline = fgetl(fid);
while ischar(tline)
disp(tline)
tline = fgetl(fid);
end
fclose(fid);
However, this would not do what you want. You should rather use fscanf to read data in the format you want. You want to read two consecutive strings (first name, last name) and an integer number. So you can use
A = fscanf(fid, '%s %s %d', [3 inf]);
to read three items at a time and repeat until the end of the file.
I answered my own question earlier today, but here's what I found if anyone is interested:
you have to index a line of string by using curly brackets instead of straight ones.
i.e.:
for index = 1:1:25
firstname{index} = fgetl(fid1);
end
fprintf('%10s', firstn{index});
fprintf will print whichever number index is supplied.
thanks anyway kavka :]

Resources