Read specific column from .mat file in matlab - matlab

I want to load specific column form .mat file already exist in the work space after I load it how should I write then ?

You can access part of a variable saved in a MAT file using MATFILE constructor (available since R2011b). Check my answer to another similar question.
For example, if you have matrix a saved in matlab.mat file you constract the matfile object as
matObj = matfile('matlab.mat');
Then you can access 2nd column like x = matObj.a(:,2);.

Related

How to save a mat file in another directory in matlab

I want to save a matrix (e.g. "PTX_Data_Raw.mat") in another folder (e.g. Temp folder). I have written below code:
mkdir('D:\Projects\ProgrammingPart\Method2_FinalApproved\8-congruent','Temp');
filename=('D:\Projects\ProgrammingPart\Method2_FinalApproved\8-congruent\Temp');
save(filename,'PTX_Data_Raw.mat');
but it didn't work. Does anybody can help me for solving this problem?
THX
Going with your comments, you are using save wrong. The first parameter is the filename you want to call the MAT file and second parameter and onwards are the variables you want to save.
Therefore, you need to make sure filename contains the entire filename, including the path followed by the actual name of the MAT file you want. After, the second parameter is PTX_Data - the name of the matrix you want to save.
mkdir('D:\Projects\ProgrammingPart\Method2_FinalApproved\8-congruent','Temp');
%// Change
filename=('D:\Projects\ProgrammingPart\Method2_FinalApproved\8-congruent\Temp\PTX_Data_Raw.mat');
save(filename,'PTX_Data'); %// Change

Convert dataset of .mat format to .csv octave/matlab

there are datasets in .mat format in the this site: http://www.cs.nyu.edu/~roweis/data.html
I want to change the format to .csv.
Can someone tell me how to change the format to create the .csv file.
Thanks!
Suppose that the .mat files from the site are available already. In the command window in Matlab, you may write, for example:
load('C:\Users\YourUserName\Downloads\mnist_all.mat');
to load the .mat file; the result should be a set of matrices test0, test1, ..., train0, train1 ... created in your workspace, which you want saved as CSV files. Because they're different size, you need to save one CSV per variable, e.g. (also in the command window):
csvwrite('C:\Users\YourUserName\Downloads\mnist_test0.csv', test0);
Repeat the command for each variable, and do not forget to change also the name of the output file to avoid overwriting.
Did you tried the csvwrite function in Matlab?
Just load your .mat files with the load function and then write them with csvwrite!
I do not have a Matlab license so I installed GNU Octave 4.2.1 (2017) on Windows 10 (thank you to John W. Eaton and others). I was not fully successful using the csvwrite so I used the following workaround. (BTW, I am totally incompetent in the Octave world. csvwrite worked for simple data structures).
In the Command Window I used the following two commands
load myfile.mat
save("-text","myfile.txt","variablename")
When the "myfile.mat" is loaded, the variable names for the data vectors loaded are displayed in the workspace window. This is the name(s) to use in the save command. Some .mat files will load several data structures.
The "-text" option is the default, so you may not need to include this option in the command.
The output file lists the .mat file contents in text format as single column (of potentially sequential variables). It should be easy to use you text editor to massage this data into the original matrix structure for use in whatever app you are comfortable with.
Had a similar issue. Needed to convert a series of .mat files that had two columns of numerical data into standard data files (ascii text). Note that I don't really ever use csv, but everything here could be adapted by using csvwrite instead of the standard save.
Using Octave 4.2.1 ....
load myfile.mat
LI = [L, I] ## L and I are column vectors representing my data
save myfile.txt LI
Note that L and I appear to be default variable names chosen by Octave for the two columns vectors in my original data file. Ideally a script that iterated over all files with the .mat extension in my directory would be ideal, but this got the job done. It saves the data as two space separated columns of data.
*** Update
The following script works on Octave 4.2.1 for a series of data files with the .mat extension that are in the same directory. It will iterate over them and write the data out to text files with the same name but with the extension .dat . Note that this is not efficient, so if you have a lot of files or if they are large it can take a while to run. I would suggest that you run it from the command line using octave mat2dat.m so you can actually watch it go.
I make no guarantees that this will work for you, but it did for me. I also am NOT proficient in Octave or Matlab, so I'm sure a better solution exists.
# mat2dat.m
dirlist = glob("*.mat")
for i=1:length(dirlist)
filename = dirlist{i,1}
load(filename, "L", "I")
LI = [L,I]
tmpname = filename(1:length(filename)-3)
txtname = strcat(tmpname, 'dat')
save(txtname, "LI")
end

Load multiple .mat files for processing

In MatLab I have (after extensive code running) multiple .mat files outputted to .mat files. The actual matlab name of each .mat file is called results but I've used the save command to write them to different files. A small subset of the files looks like this:
results_test1_1.mat
results_test1_2.mat
results_test1_3.mat
results_test1_4.mat
results_test2_1.mat
results_test2_2.mat
results_test2_3.mat
results_test2_4.mat
Now I want to compare the results for each test, which means I have to load in all four .mat files and combine them in a graph. Reading in one file and making the eventual graph is no problem. But since all files have the same matlab name results, iteratively loading them is not an option (at least, not one that I know of yet) since in the end only file 4 remains since it rewrites the previous ones.
Is there a way to load all these files and store them in different variables in a structure (regarding only one test set)? Because doing all this manually is a hell of a lot of work.
I've tried to use this method: Load Multiple .mat Files to Matlab workspace but I get an Invalid field name error on loaded.(char(file)) = load(file);
You can load into a variable (preferably a cell array)
results = cell( 2, 4 ); % allocate
for testi=1:2
for resi = 1:4
filename = sprintf('results_test%d_%d.mat', testi, resi );
results{testi,resi} = load( filename );
end
end
Now you have all the results stored in results cell array and you may access the stored variables, e.g.,
results{1,3}.someVar % access variable someVar (assuming such variable was saves to the corresponding mat file

Is it possible to add data into a matlab cell without loading the cell into the memory?

I have a large .mat file. I call it D.mat. It has a cell inside it (D.X is a cell array). I need to add more data to the end of its only column and I cannot load it into the memory.
I read about this function: memmapfile and Mapping Multiple Data Types and Arrays section, but it seems that I should know the structure of the data and in fact the D.X elements has no definite structure.
I thought maybe there is a function like fgetl for these kind of situations (I mean read the file to its last index (or row) and then write to it).
Is it possible?
Any help is appreciated.
If you have access to version R2011b or later, you can use the matfile command to load and save parts of variables in a .mat file, without loading the data into memory. See doc matfile for more information and some examples.
fgetl is not what you need: it's for reading lines from a text, not .mat file. memmapfile also requires that the data file be laid out in a regular structure, which is not the case for a .mat file.
Before R2011b there is no way I know of doing what you need.

Load a .mat file into another file without accesing variable name from .mat file

this problem is very annoying.
The situation is like this, I am having .mat file with only a single variable in it. I am interesting in loading it in another variable without accessing the variable from the file tha t I am trying to load.
Right, now I have tried something like this but with no effect:
A=[];
details=whos(FileName);
aux=load(FileName,upper(details.name);
A=aux.(details.name);
Since your MAT file has only one variable in it, you can load the whole thing and extract the name you want from there:
aux=load(FileName);
names=fieldnames(aux);
A=aux.(names{1});