Convert specific txt to mat file in Matlab - matlab

How can I convert a txt file (that has n rows of data) with the following format:
"header1","header2","header3"
1.20,2,3
2.03,3,4
1.05,5,6
8.20,9,4
etc.
into a mat file that's simply a 2xn matrix that ignores the first row and first column of the text file and essentially transposes the other columns:
2 3 5 9
3 4 6 4

There are many ways to do that, but one function in particular allows starting the read at a given row and column offset simply in the calling parameter (without having to specify headerlines or adding * in format specifier): The function dlmread
In your case, it's a simple matter of calling it with the right parameters:
M = dlmread( 'test.txt' , ',' , 1 , 1 ).' ;
M =
2 3 5 9
3 4 6 4
The nice thing about this function is it returns a double array directly (useful if you only want to read numbers), instead of a cell array like some other functions.
Note that I transposed the result at the end of the line (using the transpose operator .'), to get it the way you wanted.
Also note that the last 2 parameters are considered offset (as opposed to start row index). It means a value of 1 specify an offset of 1, so the reading will start at row index 2 (first index is 1).

Related

Extracting and arranging nested data within cell array

I need to ingest a CDF (common data format) file into MATLAB. I have used the [cdfread][1] command for this purpose. An image of my output is attached below:
When I open data_import, columns 4 and 5 are in a particular 3 x 1 format (as shown in data_import(1,4)).
My question is: Is there a simple way to extract the data for each cell in column 4, such that for the 2nd row in data_import(1,4), it gets inserted as a new column (i.e. column 5) in the original data (data_import)? Similarly, 3rd row in data_import(1,4) should be inserted as a new column (column 6) in the original data (data_import). This procedure should also be repeated in the original Column 5 data which also has a similar 3 x 1 structure within each cell.
I hope I'm not being too vague in what I am describing, but I'm really not sure what I'm supposed to do regarding the commands to call for the operation. Thank you in advance.
Your desired final output has columns which are made up of these cells converted from 3 x 1 arrays to 1 x 3 cell arrays and then concatenated for each row. It's easier to do the concatenation first with the elements the "wrong way round" and then transpose the final result:
data_import = [data_import(:,1:3) num2cell([data_import{:,4}; data_import{:,5}]') data_import(:,6:end)];

Matlab, load adjacency lists (irregular file)

I have already looked here and here, but I'm not sure I found what I need.
I have an irregular file (that represents neighbors of particles 1 to 5) that looks like that
2 3 5
1 3
1 2
1
I am want to figure out a way to load it (as 'something' called A) and do the following things:
Count the number of elements on one line (for instance size(A(1,:)) shall give me 3)
Given an array B (of size 5) select the elements of B corresponding to the indices given by a line (something like B(A(1,:)) shall give me [B(2) B(3) B(5)])
Since you want arrays with size depending on their first index, you're probably left with cells. Your cell A could be such that A{1} equals to [2 3 5] and A{2} to [1 3] in your example etc. To do this you can read your file infile by
fid=fopen(infile,'rt');
A=[];
while 1
nextline=fgets(fid);
if nextline==-1 %EOF reached
break;
end
A{end+1}=sscanf(nextline,'%d');
end
fclose(fid);
%demonstrate use for indexing
B=randi(10,5,1);
B(A{3}) %2-element vector
B(A{4}) %empty vector
Then A{i} is a vector corresponding to the ith line in your file. If the line is empty, then it's the empty vector. You can use it to index B as you want to, see the example above. Note that you should not add a newline at the very end of your infile, otherwise you'll have a spurious empty element for A. If you know in advance how many lines you need to read, this is not an issue.
And the number of entries in line i are given by length(A{i}), with i=1:length(A).

I want to extract a row element in a matrix

X =
4 3
8 3
I want to extract the element in each row of X and do some operation on each of them separably (4,3) and (8,3). however the size of may be different based on some parameters in my code, so I want general formula to do such thing,
How I can use the for loop for solving this issue ?
This link shows how to extract specific lines (or columns) from a matrix http://www.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html
All you have to do is write a loop on an index ii to go through every line.
for ii=1:size(X,1)
a=myfun(X(ii,:));
end

MATLAB: copy a specific portion of an array

I am trying to copy a few elements from a matrix, but not a whole row, and not a single element.
For example, in the following matrix:
a = 1 2
3 4
5 6
7 8
9 0
How would I copy out just the following data?
b = 1
3
5
i.e. rows 1:3 in column 1 only... I know that you can remove an entire column like this:
b = a(:,1)
... and I appreciate that could just do this and then dump the last two rows, but I'd like to use more streamlined code as I am running a very resource-intensive solution.
Elements in a matrix in MATLAB are stored in column-major order. Which means, you could even use a single index and say:
b = a(1:3);
Since the first 3 elements ARE 1,3,5. Similarly, a(6) is 2, a(7) is 4 etc. Look at the sub2ind method to understand more:
http://www.mathworks.com/help/techdoc/ref/sub2ind.html
You are not "removing" the second column, you are referencing the other column.
You should read some of the Matlab docs, they provide some help about the syntax for accessing portions of matrices:
http://www.mathworks.com/help/techdoc/learn_matlab/f2-12841.html#f2-428

Removing rows with identical first column value in matlab

I have a cell matrix of size 10000 X 3 in Matlab and I would like to remove rows with the same value in the first column.
That is, if row i and row j have the same value in the first column, I'd like to delete both rows.
I should also say that there can be more than two rows with the same value in the first column and in that case, I'd like to delete all these rows.
How do I do it?
Thanks!
You can use the functions histc, unique and logical indexing to achieve what you want. Here's a small example.
a=randi(10,5,3) %#generate a sample random matrix
a =
5 3 5
5 7 10
7 7 4
8 2 6
8 2 3
[uniqVals,uniqIndx]=unique(a(:,1)); %# get unique values and corresponding indices of the first column of a
count=histc(a(:,1),uniqVals); %# get the bin counts of the elements (i.e., find which are repeated)
b=a(uniqIndx(count==1),:)
b =
7 7 4
Only the row with the non-repeated element is selected. Since you said that you have a cell matrix, simply covert it to a matrix using cell2mat before doing this.