How to import Stata dta file in Matlab - matlab

I have a dta file that has been produced in Stata 16. All variables are "float". I would like to import all the variables into a Matlab mat array (Matlab R2019b). How can I do that? I do not have Excel in my computer (hence, I cannot pass the variables to Excel and then load in Matlab).

Related

How to use a MATLAB file as a file source in GNU Radio

I designed a filter and applied it to a random noise signal using SPTool in MATLAB. My noise signal was x = (1/sqrt(2))*(randn(1024,1)+j*randn(1024,1))
Once I've applied my filter to this noise signal, how can I take that filtered signal and use it as a file source in GNU Radio Companion (which I will connect to QT GUI Frequency Sink)? I tried exporting the signal using SPTool but I'm unsure what file extension I can use for GNU Radio. Thanks in advance.
Use fwrite with the right precision parameter that gives you float 32 binaries.
Or just use the octave/Matlab scripts in GNU Radio that do exactly that: write raw binary data. For more info, see the GNU Radio FAQ entry on the file format. (On https://wiki.gnuradio.org )
If you have a .mat file, another option is to use a Vector Source block putting in the Vector field:
loadmat('filename')['varname'].flatten()
For this to work you need to:
import numpy
from scipy.io import loadmat
Note this loads the whole file in memory (but you can specify which variables to load from the file if that's a problem). Also if the data is stored as float64 (as is the norm in Matlab) then I think there could be some rounding/truncation since GR Vector Source expects float32 (don't know enough about how SWIG handles that).

Import or load ".dat" format file in matlab (if matrix size is 1000x1000) & loaded variable should also be in matrix format

I am trying to load a data file '.dat' format in matlab script.
A=[1234567890;1234567890;1234567890;1234567890;1234567890;1234567890]
C= importdata('A.dat')
but its not working.
result matrix after import arrange all the values in only two columns.
how i load a 1000x1000 matix in the code script?

Weka from Matlab : Attribute Selection

I added the Weka.jar file to my environment variables and can already loadARFF files into Matlab and read the instances. Now I would like to perform batch attribute selection on a set of training and testing pairs of files but I cannot seem to find any tutorials on how to do that from Matlab.
I do not want to do it through weka command line because I have a set of 15 training files, and 15 test files for only one trial (and I have many trials with different ARFF files) hence I wanted to loop through them fast from Matlab.
Your help will be highly appreciated :) thanks!
Two things:
First, if you are in the MATLAB Desktop (the MATLAB console) and you preface your command with an exclamation point (!) then you can run command-line arguments. This works in scripts (.m MATLAB files) too.
Ex:
>> !man ls
What this means is that the things you can do in a terminal (like from this tutorial), you should be able to do in MATLAB.
Second, you can access Java libraries from MATLAB. You can access functions from weka.attributeSelection by importing it into your MATLAB workspace and then using the methods you need as you would do in java. For example, here's a .m file written by Matthew Dunham that imports a weka library (weka.core.converters.ArffLoader) and uses it in a .m file:
function wekaOBJ = loadARFF(filename)
% Load data from a weka .arff file into a java weka Instances object for
% use by weka classes. This can be converted for use in matlab by passing
% wekaOBJ to the weka2matlab function.
%
% Written by Matthew Dunham
if(~wekaPathCheck),wekaOBJ = []; return,end
import weka.core.converters.ArffLoader;
import java.io.File;
loader = ArffLoader();
loader.setFile(File(filename));
wekaOBJ = loader.getDataSet();
wekaOBJ.setClassIndex(wekaOBJ.numAttributes -1);
end

Matlab how to open file in excel put into array and convert numbers to units of measurement

I have an excel file to open matlab and put into an array of cells - then take the numbers in the cells and convert a few measurements. how do i do this
I'd suggest looking into the two functions xlsread and xlswrite These both handle input (from an .xls or .xlsx) file to matlab and output from matlab to an excel file, respectively. If you're looking to do something different than that, please elaborate a bit more than what you've posted.

How to I give input through a file in MATLAB?

I have a data file having 50 2-D data points written in Notepad. I want to use it in clustering algorithm to cluster these 50 points. How can I import this file? Is there any other way to use it in program?
You can save the data as a .csv file or you can save it to an excel spreadsheet and use xlsread(). See here for more info: http://www.mathworks.com/help/techdoc/ref/xlsread.html
For the .csv case, this post should prove helpful: Fastest way to import CSV files in MATLAB
Imagine you had the following data:
X = [randn(100,2)-1 ; randn(100,2)];
save data.mat X
Then its as simple as doing:
%# load data from MAT-file
load data.mat
%# cluster into K=2 clusters
C = kmeans(X,2);
%# show cluster assignment
gscatter(X(:,1), X(:,2), C)
It depends how you have formatted the data file. You say it is saved on notepad but that is not too helpful. Depending on what you have used as the data delimiter you can import the datafile into an array using the dlmread function. For example if your file is called filename.dat and have used a ; character to separate each data item within this file you could read the data into a matrix A using
A = dlmread("filename.dat",';');
I would suggest reading the help documentation on the dlmread function in matlab.