Insert variable in a string, matlab - matlab

I am analyzing some data and I need to load the information from a file. I wrote a script which contains the following line:
load('../Psychopy/DataPrueba/Estefi_1_datos.mat');
I'm wondering if there's anyway to write something like:
Name=Estefi;
load('../Psychopy/DataPrueba/Name_1_datos.mat');
Because it's data from an experiment, I have to do with 40 people at least and that little change in the script would automatize my work quite a lot.

You can do it like this:
Name='Estefi';
load(['../Psychopy/DataPrueba/',Name,'_1_datos.mat']);
Two things to note:
The variable Name has to be a string (use ')
You have to use square brackets ([]) around the combination of strings and your variable.

Related

Latex variable automation: writing variables for Latex directly from Stata and MATLAB

I am working on this project which requires analyzing a large (>50GB) dataset in a server, both in Stata and MATLAB. Both parts are required and I cannot use only one of them.
My ultimate goal is to generate a .tex file named something like commands.tex which looks like this:
\newcommand{\var1}{val1}
\newcommand{\var2}{val2} % MATLAB file matlab_file.m on DD/MM/YYYY
\newcommand{\var3}{val3} % Stata file stata_file.m on DD/MM/YYYY
...
where variables are ordered alphabetically and each of the values is most probably a number. Note that the commands in the comments would help me trace where did I generate the values. The usage of the file is so that after a preamble I can use LaTeX on the following way:
<preamble>
\input{commands.tex}
\begin{document}
Variable 1 has a value of \var1 and variable 2 has a value of \var2.
\end{document}
The purpose of this is so that I can analyze locally (or remotely) a sample, say of 0.1 or 10 percent of the total observations, write a report with those, and then run the analysis again with a bigger size. I want to completely eliminate the chances of me copying a number wrong.
I am trying to write some code both in MATLAB and Stata, but I think that is beyond my expertise, and would be very grateful if someone could help me figure out how to do it. To be honest, I feel I would be able to do the MATLAB part but the Stata I have no idea.
Stata code
What I am trying to do is to generate a command that takes as an input a name and a scalar and as an output defines the corresponding variable in my commands.tex file detailed above. My goal is to be able to generate something like this:
sysuse auto
reg price weight
define_variable PriceWeight = _b[weight], format(%4.2f)
and what I hope the code to do is that:
If \newcommand{\PriceWeight} does not exist in commands.tex then it adds its value to the list, preserving the alphabetical order.
If the variable exists then it deletes its value and rewrites above it, with the value given in the scalar.
I know how to give the values to a program in Stata, but I do not exactly know how to use those values and perform the necessary commands. The syntax is something like:
program define define_variable
syntax anything = X, [format(string)]
<other code>
end
Note: Of course, I need something way deeper than regression coefficients, but as a simple example this would suffice.
MATLAB code
This seems to be easier in MATLAB, but I do not know exactly how to automate the process. In MATLAB what I want to be able to do is something like:
clc; clear;
PriceWeight = 3
define_variable('PriceWeight',PriceWeight,format)
again where it automatically goes to the single file and updates it accordingly. Any hel[p with be very much appreciated.
Based on your comments and assuming that your file with all relevant variables is not huge, I would suggest getting your data from Stata to Matlab, and update your variables there as necessary (using functions such as exist or strcmp if you have a list of names). A quick google search gives me this link for Stata to Matlab.
To make it easy to process you might want to create a cell (I will call C), where one column contains all variable names and one column contains the scalar values.
Then, once you have assembled all your variables, you can sort your cell alphabetically and write it to a file using this.. Of course you would write a .tex file, and then iterate over your cell with something like
fprintf(fID,'\newcommand{\%s}{%f} ',C{i,1},C{i,2})
I hope this is understandable and helps.

SPSS issue with RENAME VARIABLES

I have a long syntax (1800 lines) and this one portion has been giving me trouble. I can't for the life of me figure out what I'm doing incorrectly.
It is supposed to take an existing file and narrow it down to just the variables listed in the /KEEP statement. Then every variable is renamed to a similar variable name, but "oldxxxx". Later my syntax matches the new file to this updated variable file and points out any changes in the values, giving a list of reasons in the recoded file.
Once the syntax reaches the first RENAME VARIABLES I get the following error:
RENAME VARIABLES Duplicate variable names from RENAME.
Thank you in advance!
First a couple of remarks: It would be better practice to save to a different file name. In your syntax the original file gets saved over and you can't go back... Also I recommend you follow #Andy W's advice regarding how to keep only the variables you need in your file.
Now, in the sample syntax you posted I see an error - possibly that's your problem:
RENAME VARIABLES (total_EMFASYS_award=oldgrant).
The new name is oldgrant instead or oldtotal_EMFASYS_award. Possibly further down you've got another command saying
RENAME VARIABLES (grant=oldgrant).
hence the double name.
To avoid such errors and shorten your syntax, you could use the following macro:
define renVars (!pos=!cmdend)
rename variables
!do !i !in (!1) !i = !concat("old",!i)
!doend .
!enddefine.
After running this macro definition you can run the macro by stating the macro name and the full list of variables you want renamed, like this:
renVars
Student_ID rl_highschoolgpa comb need qualitygrp NewUpfrontGrant meritgrant
targetcounty_housing housinggrant tuitiongrant athlete_recruit .
One thing to note about RENAME VARIABLES command - it also works like this:
RENAME VARIABLES (list_of_starting_variable_names = list_of_final_variable_names).
you would just need to provide the 2 names lists, and the renaming will be done in th eorder in which the names are provided (1st variable in list 1 gets renamed to the 1st variable in list 2,... n-th variable in list 1 into the n-th variable in list 2... and so on.
This should avoid the Duplicate Variable Names error you are getting, as all renames are done in one go. But would require you to alter the original syntax a bit, and is a bit harder to spot which variable gets renamed into which variable.

Script for running (testing) another matlab script?

I need to create a matlab mfile that will run another matlab file with default values given in a txt file. It's ment to be useful for testing programs, so that user may specify values in a txt files and instead of inputing values every time he starts the program, my script will give the program default values and user will only see the result.
My idea is to load tested file into a variable, change 'variable=input('...');' for variable = default_variable;, save it to tmp file, execute, and than delete tmp file. Is this going to do the job?
I have only two problems:
1) How to eliminate the problem of duplicated variable names - i mean this must work for all scripts, i don't know the names of variables used in tested script.
2) As I wrote before - is this going to work fine? Or maybe I missed a easier way to do it - for example maybe I don't have to create a tmp file?
I really need your help!
Thanks in advance!
If the person who has to edit the default values has access to Matlab, I would recommend saveing the values in a mat file and loading them when needed. Otherwise you could just write a smalls cript that contains the assignment to certain variables, but make sure to keep this small. For example:
maxRuns = 100;
clusters = 12;
So much for setting up the defaults. Regarding the process my main advice is to wrap the thing that you want to test into a function. This way variables used in the code to call the 'script' will not interfere as a function gets its own separate workspace. Check doc function if you are not familiar with them.

Changing value of a variable in perl using another script

I have an unusual requirement. I have a big config /perl file in which I would like to change the value of one variable before my run. To avoid manually finding the variable and changing it's value, I would like to write a perl script to change the name of the variable. Is that possible to do this without parsing every single line of big perl file, creating a temporary copy and overwriting old file.
Something is parsing this file at some point, right? Give it a list of things to substitute and you can have it only do the substitutions when it needs it. This avoids a big pre-startup overhead and if the config file is sparsely used, will result in a faster overall run.
So just make the thing reading it look for certain patterns to substitute in and a file (or passed in on the command line or environment variables, or...) for the values it should use and go from there.
If you don't have control over the parser, then there's not much to do. You could one-time pre-process the config file to determine EXACTLY where the substitutions need to be and write a faster processor, since it won't have to do any string parsing for regular expressions, just moving a bunch of bytes as fast as your computer can move them to the new file with the substitutions in place.

Reading large csv files with strings containing commas as one field

I have a large .csv file (~26000 rows). I want to be able to read it into matlab. Another problem is that it contains a collection of strings delimited by commas in one of the fields.
I'm having trouble reading it. I tried stuff like tdfread, which won't work here. Any tricks with textscan i should be aware about?
Is there any other way?
I'm not sure what is generating your CSV file but that is your problem.
The point of a CSV file, is that the file itself designates separation of fields. If the text of the CSV contains commas, then nothing you can do will help you. How would ANY program know when the text in a single field contains commas, or when that comma is a field delimiter?
Proper CSV would have a text qualifier. Some generators/readers gives you the option to use one. The standard text qualifier is a " (quote). Its changeable, though, because your text may contain those, too.
Again, its all about generating proper CSV content.
There's a chance that xlsread won't give you the answer you expect -- do the strings always appear in the same columns, for example? I think (as everyone else seems to :-) that it would be more robust to just use
fid = fopen('yourfile.csv');
and then either textscan
t = textscan(fid, '%s', delimiter', sprintf('\n'));
t = t{1};
or just fgetl (the example in the help is perfect).
After that you can do some line-by-line processing -- using textscan again on the text content of each line, for example, is a nice, quick way to get a cell-array that will allow fast analysis of each line.
You have a problem because you're reading it in as a .csv, and you have commas within your data. You can get it in Excel and manipulate the date, possibly extract the unwanted commas with Excel formulas. I work with .csv files for DB imports quite a bit. I imagine matLab has similar rules, which is - no commas in your data.
Can you tell us more about your data? Are there commas throughout, our just one column? Maybe you can read it in as tab delimited?
Are you using a Unix system? The reason I am asking is that you could use a command-line function such as sed and regular expressions to clean those data files before you pass them into Matlab. Here is a link that explains how to do exactly what you are looking for.
Since, as others have observed, your file is CSV with commas inside what you think of as a single field, it's going to be hard to persuade Matlab that that really is only one field. I think your best strategy is going to be to read one line at a time, into a string acting as a buffer, and to translate it, field-by-field, into the variables or other data structures that you want. Since Matlab has in-built regular expression capabilities this shouldn't be too hard.
And, as others have already suggested, posting a sample of your data would help us to help you.
One easy solution is:
path='C:\folder1\folder2\';
data = 'data.csv';
data = dataset('xlsfile',sprintf('%s\%s', path,data));
Of course you could also do the following:
[data,path] = uigetfile('C:\folder1\folder2\*.csv');
data = dataset('xlsfile',sprintf('%s\%s', path,data));
now you will have loaded the data as dataset. An easy way to get a column 1 for example is
double(data(1))